美文网首页
iOS通知相关

iOS通知相关

作者: 山有木枝壮 | 来源:发表于2019-10-14 17:14 被阅读0次

    远程推送

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

    • 如果未开启该功能,在获取deviceToken的时候不会执行下面方法,会执行对应的error方法
    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

    相关文章

      网友评论

          本文标题:iOS通知相关

          本文链接:https://www.haomeiwen.com/subject/zvvrmctx.html