我想在我的应用程序中添加UNNotificationAction,我已经使用以下代码完成了推送通知: -
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
UNNotificationAction *accept = [UNNotificationAction actionWithIdentifier:kAcceptIdentifier
title:NSLocalizedString(@"Accept", nil)
options:UNNotificationActionOptionForeground];
UNNotificationAction *reject = [UNNotificationAction actionWithIdentifier:kRejectIdentifier
title:NSLocalizedString(@"Reject", nil)
options:UNNotificationActionOptionForeground];
NSArray *buttons = @[ accept, reject ];
// create a category for message failed
UNNotificationCategory *buttonsAction = [UNNotificationCategory categoryWithIdentifier:@"Chat_Request"
actions:buttons
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObjects:buttonsAction, nil];
// registration
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}];
但现在我希望这些操作出现在从服务器收到的某些特定推送通知中。目前它正在显示所有通知。我不知道该怎么做。
提前致谢。
答案 0 :(得分:0)
我自己用以下代码解决了这个问题:
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
UNNotificationAction *accept = [UNNotificationAction actionWithIdentifier:kAcceptIdentifier
title:NSLocalizedString(@"Accept", nil)
options:UNNotificationActionOptionForeground];
UNNotificationAction *reject = [UNNotificationAction actionWithIdentifier:kRejectIdentifier
title:NSLocalizedString(@"Reject", nil)
options:UNNotificationActionOptionForeground];
NSArray *buttons = @[ accept, reject ];
// create a category for action
UNNotificationCategory *buttonsAction = [UNNotificationCategory categoryWithIdentifier:@"Chat_Request"
actions:buttons
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObjects:buttonsAction, nil];
// registration
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}];
确保您的有效负载必须包含与您的代码中具有相同标识符的“category”键。例如:
{"aps":{"alert":"Testing2","badge":1,"sound":"default","category":"Chat_Request"}}