从未称呼UNUserNotificationCenter完成为Swift

Mar*_*eli 2 swift unusernotificationcenter

我的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()


    }
Run Code Online (Sandbox Code Playgroud)

我的通知出现,当我单击通知以打开应用程序时,该应用程序将打开,控制台显示以下内容:

应用已打开

2017-07-23 14:48:56.182表格[3255:198009]警告:UNUserNotificationCenter委托收到了对-userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler的调用:但从未调用完成处理程序。

所以看起来我的打印出来显示“ App is open”,但我正在下面看到完成处理程序错误。

Nar*_*yan 5

在以下代码中添加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()
}
Run Code Online (Sandbox Code Playgroud)