我的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是开放的 2017-07-23 14:48:56.182 call 3255:198009警告: UNUserNotificationCenter委托收到了对UNUserNotificationCenter的调用,但未调用完成处理程序。
因此,我的打印输出"App“显示,但我得到了它下面的完成处理程序错误。
发布于 2017-07-23 19:17:57
在代码中添加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()
}发布于 2017-07-23 19:18:04
您需要确保在方法中调用完成处理程序,以使系统知道处理通知的工作已经完成。
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. . .
// Execute completion handler
completionHandler()
}https://stackoverflow.com/questions/45268927
复制相似问题