iOS被杀死时通知不显示带操作按钮

时间:2018-10-05 10:20:35

标签: ios xcode unnotificationrequest

当使用一个信号发出通知时,我已经完成了以下代码来显示操作按钮。但是当应用程序被杀死时,通知不会到来。它在背景/前景模式下正常工作。但是,当应用程序不在托盘中时,通知甚至停止发出。没有动作按钮,它运行完美。

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 个答案:

答案 0 :(得分:2)

在Push Notification中有两种方法可以实现此操作处理。 1)简单地注册您当前正在使用的UNUserNotificationCenter,但您遇到的问题是操作按钮未以终止模式显示。

因此,请使用您共享的配置检查有效载荷用户信息字典标识符和类别。

以下是我的代码,请检查以下Swift代码。

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])
}

我更新了您的代码,请检查此

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]];

更改只是不设置选项。

2)使用通知扩展您可以设计有吸引力的推送通知。为此,我建议选择此博客documentation