Reputation: 207
I want to add UNNotificationAction in my application on push notification which i have done with following code:-
[[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];
}];
But now I want these actions to appear on some particular push notifications received from server. Currently it is showing on all notifications. I am not sure how to do it.
Thanks in advance.
Upvotes: 0
Views: 755
Reputation: 207
I solved it myself with following code:
[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];
}];
Make sure your payload must contain "category" key with same identifier as yours in your code. For example:
{"aps":{"alert":"Testing2","badge":1,"sound":"default","category":"Chat_Request"}}
Upvotes: 1