Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果应用程序由本地通知启动,则访问通知有效负载_Ios_Swift_Uilocalnotification - Fatal编程技术网

Ios 如果应用程序由本地通知启动,则访问通知有效负载

Ios 如果应用程序由本地通知启动,则访问通知有效负载,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,当应用程序处于挂起状态并启动应用程序时,如果用户单击本地通知,我如何获得通知并访问通知负载? 无论何时从后台状态打开我的iOS应用程序,都会调用UNNotification response函数。由key启动的本地通知已被弃用,我无法确定该应用程序是否由本地通知启动。我需要使用通知负载来正确填充视图控制器 我使用此功能在应用程序处于后台状态时获取通知响应: func userNotificationCenter(_ center: UNUserNotificationCenter, di

当应用程序处于挂起状态并启动应用程序时,如果用户单击本地通知,我如何获得通知并访问通知负载? 无论何时从后台状态打开我的iOS应用程序,都会调用UNNotification response函数。由key启动的本地通知已被弃用,我无法确定该应用程序是否由本地通知启动。我需要使用通知负载来正确填充视图控制器

我使用此功能在应用程序处于后台状态时获取通知响应:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive
        response: UNNotificationResponse, withCompletionHandler completionHandler:
        @escaping () -> Void) {

setup view here using payload
}

问题可能是您没有足够早地配置UnuseNotificationCenterDelegate。您需要很早就这样做,就像在
didfishLaunching
中一样,这样运行时就知道在哪里发送
didceive
。我就是这样做的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
    let center = UNUserNotificationCenter.current()
    center.delegate = self.notifHelper
    return true
}

副本?我不明白问题是什么
didReceive
被调用,现在您可以开始比赛了。这和你以前做的有什么不同?我可能错过了什么;你能详细说明一下吗?THX如果在选择通知之前强制关闭应用程序,则不会调用该函数。如果我将应用程序设置为后台并单击通知,则会调用所实现的功能,并且工作正常。为什么不调用它?您在哪里配置用户通知中心代表?我希望知道原因。我快发疯了。我在一个静态类中有notification center委托函数。它应该在应用程序代理中吗?这不是它在哪里的问题,而是它在什么时候的问题。您需要在启动时将一些实例设置为委托。