iOS10全新方法实现推送+deviceToken无法获取或无效的解决

时间:2025-01-30 07:57:13
- ( void )registerNotification{
     /*
      identifier:行为标识符,用于调用代理方法时识别是哪种行为。
      title:行为名称。
      UIUserNotificationActivationMode:即行为是否打开APP。
      authenticationRequired:是否需要解锁。
      destructive:这个决定按钮显示颜色,YES的话按钮会是红色。
      behavior:点击按钮文字输入,是否弹出键盘
      */
     UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@ "action1" title:@ "策略1行为1" options:UNNotificationActionOptionForeground];
     /*iOS9实现方法
     UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
     = @"action1";
     =@"策略1行为1";
     = UIUserNotificationActivationModeForeground;
     = YES;
      */
     
     UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@ "action2" title:@ "策略1行为2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@ "textInputButtonTitle" textInputPlaceholder:@ "textInputPlaceholder" ];
     /*iOS9实现方法
         UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
         = @"action2";
         =@"策略1行为2";
         = UIUserNotificationActivationModeBackground;
         = NO;
         = NO;
         = UIUserNotificationActionBehaviorTextInput;//点击按钮文字输入,是否弹出键盘
     */
     
     UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@ "Category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[@ "action1" ,@ "action2" ] options:UNNotificationCategoryOptionCustomDismissAction];
     //        UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
     //         = @"Category1";
     //        [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)];
     
     UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@ "action3" title:@ "策略2行为1" options:UNNotificationActionOptionForeground];
     //        UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
     //         = @"action3";
     //        =@"策略2行为1";
     //         = UIUserNotificationActivationModeForeground;
     //         = YES;
     
     UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@ "action4" title:@ "策略2行为2" options:UNNotificationActionOptionForeground];
     //        UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init];
     //         = @"action4";
     //        =@"策略2行为2";
     //         = UIUserNotificationActivationModeBackground;
     //         = NO;
     //         = NO;
     
     UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@ "Category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[@ "action3" ,@ "action4" ] options:UNNotificationCategoryOptionCustomDismissAction];
     //        UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];
     //         = @"Category2";
     //        [category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)];
     
     
     [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[ NSSet setWithObjects:category1,category2, nil ]];
     [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^( BOOL granted, NSError * _Nullable error) {
         NSLog (@ "completionHandler" );
     }];
     /*iOS9实现方法
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];
 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
     */
     [[UIApplication sharedApplication] registerForRemoteNotifications];
     
     
     [UNUserNotificationCenter currentNotificationCenter].delegate = self ;
}