首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNUserNotificationCenter完成从未被称为Swift

UNUserNotificationCenter完成从未被称为Swift
EN

Stack Overflow用户
提问于 2017-07-23 19:07:53
回答 2查看 5.4K关注 0票数 2

我的AppDelagate中有以下代码块:

代码语言:javascript
复制
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“显示,但我得到了它下面的完成处理程序错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-23 19:17:57

在代码中添加completionHandler()

userNotificationCenter

代码语言:javascript
复制
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()
}
票数 5
EN

Stack Overflow用户

发布于 2017-07-23 19:18:04

您需要确保在方法中调用完成处理程序,以使系统知道处理通知的工作已经完成。

代码语言:javascript
复制
  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()
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45268927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档