gpt4 book ai didi

ios - 当应用程序被杀死时,操作按钮不显示通知 ios

转载 作者:行者123 更新时间:2023-11-28 20:55:37 28 4
gpt4 key购买 nike

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

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) 注册您当前正在使用的 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)使用通知扩展您可以设计有吸引力的推送通知。为此,我建议首选此博客 iOS Remote Push Notifications

关于ios - 当应用程序被杀死时,操作按钮不显示通知 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663440/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com