使用极光推送
按照极光官网文档进行配置
APNs 通知:是指通过向 Apple APNs 服务器发送通知,到达 iOS 设备,由 iOS 系统提供展现的推送。用户可以通过 IOS 系统的 “设置” >> “通知” 进行设置,开启或者关闭某一个 App 的推送能力。JPush iOS SDK 不负责 APNs 通知的展现,只是向 JPush 服务器端上传 Device Token 信息,JPush 服务器端代理开发者向 Apple APNs 推送通知。
应用内消息:JPush iOS SDK 提供的应用内消息功能,在 App 在前台时能够收到推送下来的消息。App 可使用此功能来做消息下发动作。此消息不经过 APNs 服务器,完全由 JPush 提供功能支持。
向苹果注册远程通知
- (void)registerRemoteNotification
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
if (IOS_VERSION >= 10.0) {
UNAuthorizationOptions authorizationOptions = UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound;
void (^completionHandler)(BOOL granted, NSError *_Nullable error) = ^(BOOL granted, NSError *_Nullable error) {
UPLogDebug(@"UPPush", @"%s[%d]granted=%@", __PRETTY_FUNCTION__, __LINE__, granted ? @"YES" : @"NO");
};
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authorizationOptions
completionHandler:completionHandler];
return;
}
#endif
if (IOS_VERSION >= 8.0) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
}
极光本地配置
根据appkey以及LuanchOption初始化JPushService.。
- (void)startWithAppKey:(NSString *)appKey
option:(NSDictionary *)launchOptions
apsForProduction:(BOOL)isProduction
{
//注册收到非apns,即自定义信息
[self addNetworkReceiveObserver];
//测试检测极光连接状态
[self addJPFNetworkObserver];
//祖册远程通知
[self registerForRemoteAPNSNotificationTypes];
#if !TARGET_OS_SIMULATOR
//启动极光sdk
[JPUSHService setupWithOption:launchOptions
appKey:appKey
channel:nil
apsForProduction:isProduction];
//打开日志级别到Debug
[JPUSHService setDebugMode];
#endif
}
获取极光推送状态
根据极光定义状态,添加状态通知,检测并打印极光推送连接状态
#if !TARGET_OS_SIMULATOR
#pragma mark - DEBUG
extern NSString *const kJPFNetworkIsConnectingNotification; // 正在连接中
extern NSString *const kJPFNetworkDidSetupNotification; // 建立连接
extern NSString *const kJPFNetworkDidCloseNotification; // 关闭连接
extern NSString *const kJPFNetworkDidRegisterNotification; // 注册成功
extern NSString *const kJPFNetworkFailedRegisterNotification; //注册失败
extern NSString *const kJPFNetworkDidLoginNotification; // 登录成功
extern NSString *const kJPFNetworkDidReceiveMessageNotification; // 收到消息(非APNS)
extern NSString *const kJPFServiceErrorNotification; // 错误提示
#endif
- (void)addJPFNetworkObserver
{
#if !TARGET_OS_SIMULATOR
[self addObserverWithName:kJPFNetworkIsConnectingNotification];
[self addObserverWithName:kJPFNetworkDidSetupNotification];
[self addObserverWithName:kJPFNetworkDidCloseNotification];
[self addObserverWithName:kJPFNetworkDidRegisterNotification];
[self addObserverWithName:kJPFNetworkFailedRegisterNotification];
[self addObserverWithName:kJPFNetworkDidLoginNotification];
[self addObserverWithName:kJPFServiceErrorNotification];
#endif
}
- (void)addObserverWithName:(NSString *)notiName
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(JPFNetworkStatus:)
name:notiName
object:nil];
}
- (void)JPFNetworkStatus:(NSNotification *)noti
{
NSLog(@"<推送调试>极光SDKnetwork状态:%@", noti.name);
}
实现sdk中定义方法
- (void)registrationIDCompletionHandler:(void (^)(int resCode, NSString *registrationID))completionHandler
{
#if !TARGET_OS_SIMULATOR
[JPUSHService registrationIDCompletionHandler:completionHandler];
#endif
}
- (void)registerDeviceToken:(NSData *)deviceToken
{
#if !TARGET_OS_SIMULATOR
[JPUSHService registerDeviceToken:deviceToken];
#endif
}
- (void)appDidReceiveRemoteNotification:(NSDictionary *)userInfo
{
#if !TARGET_OS_SIMULATOR
if ([userInfo objectForKey:@"content"]) {
[self handleMessage:userInfo];
}
[JPUSHService handleRemoteNotification:userInfo];
#endif
}
appdelegate中通知处理
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[UPContext sharedInstance].notificationSettings = notificationSettings;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidRegisterUserNotificationSettings];
}
#endif
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[UPContext sharedInstance].remoteNotificationRegistrationError = error;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidFailToRegisterForRemoteNotificationsEvent];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[UPContext sharedInstance].deviceToken = deviceToken;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidRegisterForRemoteNotificationsEvent];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[UPContext sharedInstance].userInfo = userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveRemoteNotificationEvent];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[UPContext sharedInstance].userInfo = userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveRemoteNotificationEvent];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[UPContext sharedInstance].userInfo = notification.userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveLocalNotificationEvent];
}
前台收到信息时
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[UPContext sharedInstance].userInfo = notification.request.content.userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveRemoteNotificationEvent];
}
else {
[UPContext sharedInstance].userInfo = notification.request.content.userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveLocalNotificationEvent];
}
}
#endif
后台收到消息时
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[UPContext sharedInstance].userInfo = userInfo;
[[UPModuleManager sharedInstance] handleModuleEvent:UPMDidReceiveRemoteNotificationEvent];
}
收到消息后进行解码处理
- (void)handleMessage:(NSDictionary *)message
{
NSLog(@"<推送调试>收到解码前消息%@", message);
NSString *base64Str = [message valueForKey:@"content"];
if (base64Str) {
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
if (data) {
NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"<推送调试>收到解码后消息%@", info);
if (info) {
[self.delegate jPushDidReceiveRemoteNotification:info];
}
}
}
}
根据是否开启免打扰做过滤
- (void)creatUPNMessageWithRemoteNotification:(NSDictionary *)noti
{
//消息面打扰
if ([self isNoDisturb])
return;
UPNMessage *message = [UPNMessage creatAndSaveModelFromDictionary:noti];
if (message) {
[self postNotificationWithMessage:message];
}
}
根据消息种类发送对应通知
- (void)postNotificationWithMessage:(UPNMessage *)message
{
//检查是否设置消息免打扰
if (message) {
NSString *notiName = [self notificationNameByMsg:message];
if (notiName) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:notiName object:message];
});
}
[self reportMsg:message];
}
}
- (NSString *)notificationNameByMsg:(UPNMessage *)message
{
if (message.view) {
return [self notificationNameByUPNView:message.view];
}
else if (message.extData.devControl) //devControl
{
return kUPNMessage_DeviceControl;
}
else if (message.extData.api) //api
{
return message.msgName;
}
else if (message.msgName) //other
{
return message.msgName;
}
return nil;
}
具体类中监听对应通知并实现通知方法
- (void)registerHandler
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceivedNotication:) name:kUPNMessage_PageJump object:nil];
}
#pragma mark - Non-Public Method
- (void)didReceivedNotication:(NSNotification *)noti
{
UPNMessage *message = [noti object];
if (!message) {
return;
}
self.message = message;
[self pageJumpWithMessage:message];
}