iOS10上的交互式通知(Objective-C)

时间:2018-01-04 14:28:59

标签: objective-c notifications apple-push-notifications ios10

我正在尝试在iOS10上实现交互式通知(也称为可操作通知)。 在方法中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

我写了以下代码:

   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
           center.delegate = self;

        UNAuthorizationOptions authOptions =
        UNAuthorizationOptionAlert
        + UNAuthorizationOptionSound
        + UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
             if( !error )
                              {
                                  [[UIApplication sharedApplication] registerForRemoteNotifications];  // required to get the app to do anything at all about push notifications
                                  NSLog( @"Push registration success." );
                              }
                              else
                              {
                                  NSLog( @"Push registration FAILED" );
                                  NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
                                  NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
                              }
         }
         ];


    // Create the custom actions for expired timer notifications.
    UNNotificationAction* snoozeAction = [UNNotificationAction
                                          actionWithIdentifier:@"SNOOZE_ACTION"
                                          title:@"Snooze"
                                          options:UNNotificationActionOptionNone];

    UNNotificationAction* stopAction = [UNNotificationAction
                                        actionWithIdentifier:@"STOP_ACTION"
                                        title:@"Stop"
                                        options:UNNotificationActionOptionForeground];

    // Create the category with the custom actions.
    UNNotificationCategory* expiredCategory = [UNNotificationCategory
                                               categoryWithIdentifier:@"TIMER_EXPIRED"
                                               actions:@[snoozeAction, stopAction]
                                               intentIdentifiers:@[]
                                               options:UNNotificationCategoryOptionNone];

    // Register the notification categories.

    [center setNotificationCategories:[NSSet setWithObjects: expiredCategory,
                                       nil]];

但是当我发送通知时:

{"aps":{
    "alert":"Here is an interactive notification sended to the user iPad as soon as possible through a Push message",
    "badge":10,
    "sound": "default",

     "category": "TIMER_EXPIRED",
    }
}

我只收到非交互式通知:

enter image description here

为什么我看不到交互式通知(带两个按钮)?

0 个答案:

没有答案