gpt4 book ai didi

swift - UNUserNotificationCenter Completion 从未调用过 Swift

转载 作者:可可西里 更新时间:2023-11-01 00:00:26 25 4
gpt4 key购买 nike

我的 AppDelagate 中有以下代码块:

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

if response.actionIdentifier == UNNotificationDismissActionIdentifier {
print ("Message Closed")
}
else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print ("App is Open")
}

// Else handle any custom actions. . .
}

func showMessage()

{
let notification = UNMutableNotificationContent()
notification.title = "Test"
notification.subtitle = "This is a test"
notification.body = "I need to tell you something, but first read this."
notification.categoryIdentifier = "TESTAPP"
notification.sound = UNNotificationSound.default()

let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.5, repeats: false)

let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}


func applicationDidEnterBackground(_ application: UIApplication) {

let center = UNUserNotificationCenter.current()
center.delegate = self

self.showMessage()


}

我的通知出现,当我点击通知打开应用程序时,应用程序打开并且控制台显示如下:

App is Open

2017-07-23 14:48:56.182 Form[3255:198009] Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

所以它看起来像我的打印输出“应用程序已打开”它显示但我在它下面收到完成处理程序错误。

最佳答案

在你的代码下添加completionHandler()

userNotificationCenter

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

if response.actionIdentifier == UNNotificationDismissActionIdentifier {
print ("Message Closed")
}
else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print ("App is Open")
}

// Else handle any custom actions. . .
completionHandler()
}

关于swift - UNUserNotificationCenter Completion 从未调用过 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268927/

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