UNNotificationAction未显示操作

时间:2017-03-10 08:04:17

标签: ios objective-c

我有以下显示通知的代码。然而,这是工作,它没有显示动作按钮。我想知道可能出现什么问题?

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];


    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

    [center requestAuthorizationWithOptions:options
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!granted) {
                                  NSLog(@"Something went wrong");
                              }
                          }];

    // Objective-C
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.title = @"Don't forget";
    content.body = @"Buy some milk";
    content.sound = [UNNotificationSound defaultSound];

    // Time
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30
                                                                                                    repeats:NO];

    // Objective-C
    NSString *identifier = @"UYLLocalNotification";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                          content:content trigger:trigger];

    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Something went wrong: %@",error);
        }
    }];


    // Actions
    UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Snooze"
                                                                              title:@"Snooze" options:UNNotificationActionOptionNone];
    UNNotificationAction *deleteAction = [UNNotificationAction actionWithIdentifier:@"Delete"
                                                                              title:@"Delete" options:UNNotificationActionOptionDestructive];

    // Objective-C
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory"
                                                                              actions:@[snoozeAction,deleteAction] intentIdentifiers:@[]
                                                                              options:UNNotificationCategoryOptionNone];
    NSSet *categories = [NSSet setWithObject:category];

    // Objective-C
    [center setNotificationCategories:categories];

    // Objective-C
    content.categoryIdentifier = @"UYLReminderCategory";

1 个答案:

答案 0 :(得分:0)

您在完全配置 UNMutableNotificationContent 之前创建了 UNNotificationRequest ,方法是设置 notificationCategories & categoryIdentifier

只需将以下行移至代码末尾即可解决问题:

NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                      content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Something went wrong: %@",error);
    }
}];

由于模拟器不支持3D Touch,因此最好在真实设备上进行测试。当您看到通知时,强制触摸它,您应该看到操作按钮。