Notification not displayed with action buttons when app is killed ios

2.1k views Asked by At

I've done following code to display action buttons while notification comes using one signal. But when app is killed notification is not coming. It's working properly in background/foreground mode. But when app is not in tray even notification stop coming. Without action buttons it's working perfectly.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
 {
     if( !error ) {
         [[UIApplication sharedApplication] registerForRemoteNotifications];

         NSLog( @"Push registration success." );

     } else {
         NSLog( @"Push registration FAILED" );

}];

UNNotificationCategory *modifyCategory = [UNNotificationCategory categoryWithIdentifier:CYLInviteCategoryIdentifier actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];

UNNotificationAction* snoozeAction = [UNNotificationAction
                                          actionWithIdentifier:@"ACCEPT_ACTION"
                                          title:@"Accept"
                                          options:UNNotificationActionOptionForeground];

UNNotificationAction* stopAction = [UNNotificationAction
                                        actionWithIdentifier:@"DECLINE_ACTION"
                                        title:@"Decline"
                                        options:UNNotificationActionOptionDestructive];

UNNotificationCategory* actionCategory = [UNNotificationCategory
                                               categoryWithIdentifier:@"INCOMING_CALL"
                                               actions:@[snoozeAction, stopAction]
                                               intentIdentifiers:@[]
                                               options:UNNotificationCategoryOptionNone];

[center setNotificationCategories:[NSSet setWithObjects: modifyCategory, actionCategory,
                                       nil]];
1

There are 1 answers

0
Kiran Sarvaiya On BEST ANSWER

There is two way to achieve this action handling in Push Notification. 1) Is simple register your UNUserNotificationCenter which you are currently using but you are facing issues with action button not showing in killed mode.

So please check your payload user info dictionary identifier & category with your configuration which you shared.

Following is my code please check this Swift code.

private func setNotificationCategories() {

let likeAction = UNNotificationAction(identifier: "like", title: "Like", options: [])
let replyAction = UNNotificationAction(identifier: "reply", title: "Reply", options: [])
let archiveAction = UNNotificationAction(identifier: "archive", title: "Archive", options: [])
let  ccommentAction = UNTextInputNotificationAction(identifier: "comment", title: "Comment", options: [])


let localCat =  UNNotificationCategory(identifier: "category", actions: [likeAction], intentIdentifiers: [], options: [])

let customCat =  UNNotificationCategory(identifier: "recipe", actions: [likeAction,ccommentAction], intentIdentifiers: [], options: [])

let emailCat =  UNNotificationCategory(identifier: "email", actions: [replyAction, archiveAction], intentIdentifiers: [], options: [])

UNUserNotificationCenter.current().setNotificationCategories([localCat, 

customCat, emailCat])
}

And I Updated your code please check this

UNNotificationCategory *modifyCategory = [UNNotificationCategory categoryWithIdentifier:CYLInviteCategoryIdentifier actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];

UNNotificationAction* snoozeAction = [UNNotificationAction
                                          actionWithIdentifier:@"ACCEPT_ACTION"
                                          title:@"Accept"
                                          options:[]];

UNNotificationAction* stopAction = [UNNotificationAction
                                        actionWithIdentifier:@"DECLINE_ACTION"
                                        title:@"Decline"
                                        options:[]];

UNNotificationCategory* actionCategory = [UNNotificationCategory
                                               categoryWithIdentifier:@"INCOMING_CALL"
                                               actions:@[snoozeAction, stopAction]
                                               intentIdentifiers:@[]
                                               options:[]];



[center setNotificationCategories:[NSSet setWithObjects: modifyCategory, actionCategory,
                                       nil]];

change is just don't set option.

2)Use Notification extension You can design attractive push notification. for that, I suggest prefered this blog iOS Remote Push Notifications