iOS10推送之基础知识(必看篇)

2020-01-18 16:00:19于丽

在点击事件中,如果我们不写completionHandler()这个方法,可能会报一下的错误,希望大家注意下~


Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

最后最后,我们要大家补充一下,旧版本的一些方法,方便大家扩充iOS10的通知的通知,不影响原有逻辑。


- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
 NSLog(@"iOS6及以下系统,收到通知:%@", [self logDic:userInfo]);
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {

 NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);
 completionHandler(UIBackgroundFetchResultNewData);
}

2.极光推送(需要下载最新的版本)

如果用到三方的一些平台,做推送就会更为简单。

1.注册通知的代码如下


if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
 entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
 [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
 } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  //可以添加自定义categories
  [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
              UIUserNotificationTypeSound |
              UIUserNotificationTypeAlert)
           categories:nil];
 } else {
  //categories 必须为nil
  [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
              UIRemoteNotificationTypeSound |
              UIRemoteNotificationTypeAlert)
           categories:nil];
 }

注册完成之后,我们则需要加入极光推送更新后,新加入的2个方法,这两个方法在<JPUSHRegisterDelegate>代理方法中。


/*
 * @brief handle UserNotifications.framework [willPresentNotification:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心
 * @param notification 前台得到的的通知对象
 * @param completionHandler 该callback中的options 请使用UNNotificationPresentationOptions
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler;
/*
 * @brief handle UserNotifications.framework [didReceiveNotificationResponse:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心
 * @param response 通知响应对象
 * @param completionHandler
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;