- ( 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.identifier = @"action1"; action1.title=@"策略1行爲1"; action1.activationMode = UIUserNotificationActivationModeForeground; action1.destructive = YES; */ UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@ "action2" title:@ "策略1行爲2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@ "textInputButtonTitle" textInputPlaceholder:@ "textInputPlaceholder" ]; /*iOS9實現方法 UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init]; action2.identifier = @"action2"; action2.title=@"策略1行爲2"; action2.activationMode = UIUserNotificationActivationModeBackground; action2.authenticationRequired = NO; action2.destructive = NO; action2.behavior = UIUserNotificationActionBehaviorTextInput;//點擊按鈕文字輸入,是否彈出鍵盤 */ UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@ "Category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[@ "action1" ,@ "action2" ] options:UNNotificationCategoryOptionCustomDismissAction]; // UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init]; // category1.identifier = @"Category1"; // [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@ "action3" title:@ "策略2行爲1" options:UNNotificationActionOptionForeground]; // UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init]; // action3.identifier = @"action3"; // action3.title=@"策略2行爲1"; // action3.activationMode = UIUserNotificationActivationModeForeground; // action3.destructive = YES; UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@ "action4" title:@ "策略2行爲2" options:UNNotificationActionOptionForeground]; // UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init]; // action4.identifier = @"action4"; // action4.title=@"策略2行爲2"; // action4.activationMode = UIUserNotificationActivationModeBackground; // action4.authenticationRequired = NO; // action4.destructive = NO; UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@ "Category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[@ "action3" ,@ "action4" ] options:UNNotificationCategoryOptionCustomDismissAction]; // UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init]; // category2.identifier = @"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 ; } |