Swift交互式推送通知操作失败



我已经在我的应用程序中实现了交互式推送通知,如下所示,

didFinishLaunchingWithOptions中设置

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (_, _) in }
let accept = UNNotificationAction(identifier: "accept", title: "Accept", options: [])
let deny = UNNotificationAction(identifier: "deny", title: "Deny", options: [])
let category = UNNotificationCategory(identifier: "newFriendRequest", actions: [accept, deny], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
NSLog("FR_ didReceive userNotification")
completionHandler()
}

当我在应用程序处于非活动状态时单击推送通知中的任何操作按钮时,我在控制台应用程序中收到以下错误。

FR_ didReceive userNotification
Application background launch action for notification response action deny recieved action response <BSActionResponse: 0x28043c700; error: <NSError: 0x2808847b0; domain: BSActionErrorDomain; code: 4> {
description = "The operation couldn’t be completed. (BSActionErrorDomain error 4.)";
}>

注意:如果我处理或未处理didReceive响应,就会出现此错误。

尝试这样做:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case "accept":
print("Accepted")
case "deny":
print("Denied")
default:
print("Other Action")
}
completionHandler()
}

有关更多详细信息,请参阅本教程。

最新更新