iOS9 和iOS10远程推送通知的总结

iOS10开始推送通知的变化比较大,之前对推送的处理没太注意,一直没太明白推送前台、后台、杀死状态,点击通知栏走的具体方法是什么。这次有时间重新整理测试了一下关于推送的几个代理方法。下面都是我是实测的结果,iOS9.3和iOS10和iOS12的测试机,没有iOS8的,不过iOS8和iOS9效果应该是一样的。
先说苹果原生的方法,我用的是极光的推送,所以也要说说极光升级后的方法:
我们对推送的处理主要是收到消息的时机,大体上是三种情况:前台,后台,App关闭
我先在前面总结一下,后面不想看就可以不看了
推送需要用到的一共是三个方法

1
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler{
 }
2
 - -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    //功能:可设置是否在应用内弹出通知
    completionHandler(UNNotificationPresentationOptionAlert);
}
3
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
}
  • App处于前台:这个时候收到消息执行的是方法 1、2,如果iOS10点了弹出的通知栏那么3才会执行
  • App处于后台: 都执行方法1 如果点了通知栏进入App,iOS10方法3才会执行,
  • App被杀死: 如果不点击通知栏进入App,那么三个方法都不执行,点击了通知栏 iOS10 方法3执行,iOS9方法1执行

具体说明

关注下面第一个方法就行了,,第二个基本上不会用到了

//这里方法执行的实际是:1.当App前台和App后台的时候自动执行这个方法iOS10也执行,但是iOS10以前,前台的时候不会有任何提示
					2.App关闭的时候,只有点击通知栏才会执行这个方法,如果是直接进入App,不会执行这个方法
					3.这个方法需要前台执行的时候,需要开启 Background Modes
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {
//如果需要知道点击了通知栏进入的App,需要判断当前App的生命周期
	[JPUSHService handleRemoteNotification:userInfo];
    JPLLogDebug(@"iOS7+Notification>>>:%@", [self logDic:userInfo]);
    if (application.applicationState == UIApplicationStateActive
        || application.applicationState == UIApplicationStateBackground) {//前台或者后台的时候收到的消息
        
    else {App关闭的时候收到的消息
    }

    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {
    }
    
    completionHandler(UIBackgroundFetchResultNewData);
}
//这个方法基本不会用到了
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
 
}

iOS10以后苹果把推送统一了,就是下面两个方法接收通知消息

// App前台的时候,执行这个方法 后台时不执行这个方法,所以这个时候就需要用上面的方法来处理了,并且可以通过completionHandler设置消息的提醒,可以是声音,alert或者完全不提示
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    //功能:可设置是否在应用内弹出通知
    completionHandler(UNNotificationPresentationOptionAlert);
}
// App后台或者App关闭的时候 ,点击通知栏才会执行这个方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
}

有点需要注意的地方:

  • App关闭的时候点击通知栏启动App,iOS10以前可以通过launchOptions区分,iOS10以后同样可以,而且并不影响点击通知栏的时候相关方法的执行
NSDictionary *remoteCotificationDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  • 关于消息提示,不管前台还是App关闭,iOS有一个静默推送的设置,如果设置了静默推送,那么iOS10设置的提醒方式是不管用的.
  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandleriOS10以后也会执行的,不是说iOS10有新方法了,这个方法就不执行了

关于极光

极光也主要是升级以后的两个方法
这两个方法都是iOS10以后才会执行的方法

// iOS 10 Support这个其实就是对应iOS10前台的那个方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler  API_AVAILABLE(ios(10.0)){
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    ViewController *displayController = (ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    displayController.alertLabel3.text = @"执行了willPresentNotification";
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
}
// iOS 10 Support,对应点击通知栏时候的方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    ViewController *displayController = (ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    displayController.alertLabel2.text = @"执行了didReceiveNotificationResponse";
    completionHandler();  // 系统要求执行这个方法
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值