UNUserNotificationCenter完成从未被称为Swift

时间:2017-07-23 19:07:54

标签: 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()


    }

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

  

应用已开放

     

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

所以看起来我的打印输入"应用程序已打开"它显示但我在它下面得到完成处理程序错误。

2 个答案:

答案 0 :(得分: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()
}

答案 1 :(得分:2)

您需要确保在方法中调用完成处理程序,让系统知道您已完成处理通知。

  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()
}