草庐IT

swift - UNUserNotificationCenter Completion 从未调用过 Swift

coder 2023-09-12 原文

我的 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/

有关swift - UNUserNotificationCenter Completion 从未调用过 Swift的更多相关文章

随机推荐