美文网首页
iOS12推送注册及各版本兼容

iOS12推送注册及各版本兼容

作者: 单纯的敲代码 | 来源:发表于2018-08-31 22:40 被阅读0次

    自从苹果推送以来几乎每个版本都有新特性,和注册方式的修改,于是就是欲仙欲死的去适配他,以下是自己写的注册全套兼容iOS7-iOS12。

    支持Category策略预设,欢迎交流"互喷"

    关于iOS12相关特殊参数适配见文末说明

    
    /**
     *  注册通知
     *
     */
    +(void)registerUserNotificationSettings:(UIApplication*)_application responder:(id)respond{
        
        // Set Notification
        if (@available(iOS 10.0, *)) {//iOS 10+ 通知设置
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = respond;
            UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
            if (@available(iOS 12.0, *)) {
                authOptions = authOptions | UNAuthorizationOptionProvisional | UNAuthorizationOptionCriticalAlert | UNAuthorizationOptionProvidesAppNotificationSettings;
            } 
            [center requestAuthorizationWithOptions:(authOptions)
                                  completionHandler:^(BOOL granted, NSError * _Nullable error)
             {
                 if (granted) {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         //iOS 10 及以后
                         
                         //CategoryDefault
                         //查看详情
                         UNNotificationAction *actionDetail = [UNNotificationAction actionWithIdentifier:kTCTActionShowDetail
                                                                                                   title:kTCTShowDetailTitle
                                                                                                 options:UNNotificationActionOptionForeground];
                         
                         UNNotificationCategory *categoryDefault = [UNNotificationCategory categoryWithIdentifier:kTCTCategoryDefault
                                                                                                          actions:@[actionDetail]
                                                                                                intentIdentifiers:@[@""]
                                                                                                          options:UNNotificationCategoryOptionNone];
                         
                         //CategoryReply
                         //回复
    //                     UNTextInputNotificationAction *input = [UNTextInputNotificationAction actionWithIdentifier:kTCTActionReply
    //                                                                                                          title:kTCTReplyActionTitle
    //                                                                                                        options:UNNotificationActionOptionAuthenticationRequired
    //                                                                                           textInputButtonTitle:@"发送"
    //                                                                                           textInputPlaceholder:@""];
                         //查看详情
                         UNNotificationAction *foreground = [UNNotificationAction actionWithIdentifier:kTCTActionShowDetail
                                                                                                 title:kTCTShowDetailTitle
                                                                                               options:UNNotificationActionOptionForeground];
                         
                         UNNotificationCategory *categoryReply = [UNNotificationCategory categoryWithIdentifier:kTCTCategoryReply
                                                                                                        actions:@[foreground]
                                                                                              intentIdentifiers:@[@""]
                                                                                                        options:UNNotificationCategoryOptionNone];
                         
                         [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:categoryDefault,categoryReply, nil]];
                         
                         [_application registerForRemoteNotifications];
                     });
                 } 
             }];
           
        }else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))//ios8 9通知设置
        {
            //默认策略
            
            //iOS 8/9
            UIMutableUserNotificationAction * actionDetail = [[UIMutableUserNotificationAction alloc] init];
            actionDetail.identifier = kTCTActionShowDetail;
            actionDetail.title = kTCTShowDetailTitle;
            actionDetail.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
            UIMutableUserNotificationCategory * categoryDetail = [[UIMutableUserNotificationCategory alloc] init];
            categoryDetail.identifier = kTCTCategoryDefault;
            [categoryDetail setActions:@[actionDetail] forContext:(UIUserNotificationActionContextDefault)];
            
            
            //快速回复策略
            NSMutableArray *actions = [NSMutableArray array];
    //        if (@available(iOS 9.0, *)) {//iOS 9
    //            //iOS9 回复
    //            UIMutableUserNotificationAction * actionReply = [[UIMutableUserNotificationAction alloc] init];
    //            actionReply.identifier = kTCTActionReply;
    //            actionReply.title= kTCTReplyActionTitle;
    //            //如果 activationMode = UIUserNotificationActivationModeForeground;authenticationRequired属性则被忽略;
    //            actionReply.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
    //            actionReply.authenticationRequired = NO;//不需要解锁处理
    //            actionReply.destructive = NO;
    //            actionReply.behavior = UIUserNotificationActionBehaviorTextInput;//点击按钮文字输入,是否弹出键盘
    //            [actions addObject:actionReply];
    //        }
            
            UIMutableUserNotificationAction * actionDetailiOS8 = [[UIMutableUserNotificationAction alloc] init];
            actionDetailiOS8.identifier = kTCTActionShowDetail;
            actionDetailiOS8.title = kTCTShowDetailTitle;
            actionDetailiOS8.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
            [actions addObject:actionDetailiOS8];
            UIMutableUserNotificationCategory * categoryReply = [[UIMutableUserNotificationCategory alloc] init];
            categoryReply.identifier = kTCTCategoryReply;
            [categoryReply setActions:actions forContext:(UIUserNotificationActionContextDefault)];
            NSSet *categorys = [NSSet setWithObjects:categoryDetail,categoryReply, nil];
            UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeSound
                                                                | UIUserNotificationTypeAlert
                                                                | UIUserNotificationTypeBadge
                                                                                                 categories:categorys];
            [_application registerUserNotificationSettings:notificationSettings];
        }
        else//ios7及以下
        {
    //        [_application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
    //         | UIRemoteNotificationTypeSound
    //         | UIRemoteNotificationTypeAlert];
        }
    }
    

    iOS12参数特别说明

    
    //重要提醒-----勿扰模式下依然可以收到的通知,
    //此类特殊提醒需要单独申请权限:https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
    UNAuthorizationOptionCriticalAlert
    
    //应用内显示通知设置的按钮。
    UNAuthorizationOptionProvidesAppNotificationSettings
    //实现代理方法
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification {
        // 可自定义制设置
        // Open notification settings screen in app
    }
    
    //临时授权----无需用户授权也能给用户推送的新机制,主要体现就是推送消息过来会有两个按钮,会主动让用户自己选择
    //这种机制不会给用户授权的弹窗而直接尝试给用户推送,需要注意的是临时授权推送的消息只会以隐式推送的方式展示给用户
    UNAuthorizationOptionProvisional
    

    相关文章

      网友评论

          本文标题:iOS12推送注册及各版本兼容

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