iOS通知相关

2019-10-14  本文已影响0人  山有木枝壮

远程推送

1、使用之前需要确定应用是否打开了通知功能。在项目Targert -> Capabilities -> Push Ntifications中打开通知功能。对应的开发者证书也要勾线推送权限。

didRegisterForRemoteNotificationsWithDeviceToken

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 

2、使用远程通知需要区分iOS10和iOS10之前的设备,iOS10使用了全新的通知API,UserNotifications,如下所示:

/// iOS之后
if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            NSLog(@"error = %@",error);
            if (error == nil) {
                
            } else {
                NSLog(@"error = %@",error);
            }
        }];
    } else {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];


    }
    [UIApplication.sharedApplication registerForRemoteNotifications];

3、对于通知的处理方法也不同

/// ===== iOS10之前
// 应用内收到推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
}

// 后台收到推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
}

/// iOS10之后
// 应用内收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)){
    
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

// 后台收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    
    completionHandler();
}

本地推送

本地推送也分为iOS10之前和之后,iOS10退出了全新的推送API,合并了本地推送和远程推送。
1、注册通知

if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            NSLog(@"error = %@",error);
            if (error == nil) {
                
            } else {
                NSLog(@"error = %@",error);
            }
        }];
    } else {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }
    
    // 注册远程推送,获取deviceToken
    [UIApplication.sharedApplication registerForRemoteNotifications];

2、处理通知

// iOS10之前
// 应用开启
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
   
}

iOS10之后
/// iOS10之后
// 应用内收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)){
    
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

// 后台收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    
    completionHandler();
}

iOS10之后的通知类型可以通过下面的方式判断

UNNotificationTrigger *trigger = response.notification.request.trigger;
    // 远程通知
    if ([trigger isKindOfClass:UNPushNotificationTrigger.class]) {
        
    } else {
        
    }

iOS 10中,Apple将本地Push和远程Push合二为一,使用UNNotificationTrigger类来区分,所有的Push类型都是该类型的子类;
- UNPushNotificationTrigger:远程Push;
- UNTimeIntervalNotificationTrigger:本地Push,可以设置timeInterval、repeats;
- UNCalendarNotificationTrigger:本地Push,可以设置dateComponents;
- UNLocationNotificationTrigger:本地Push,基于地理位置;

push推送格式

{
  "aps" : {
    "alert" : {
      "title" : "Title",
      "body" : "Your message here." },
    "sound" : "default",
    "badge" : 9
  }
}

参考:
1、https://juejin.im/post/5d26ce1f6fb9a07ef44430d2
2、https://www.jianshu.com/p/caf5086cda19
3、https://www.justisit.com/15147921816625.html

上一篇下一篇

猜你喜欢

热点阅读