关闭应用程序时单击通知的userInfo

时间:2018-03-27 17:22:09

标签: ios swift swift3 notifications userinfo

我正在做一个安排本地通知并保存userInfo的应用。这是好的部分。

但是当关闭应用程序时,如果出现通知并且用户单击,则不会调用该方法,我无法处理userInfo。

我看到有一种新方式可以通过UNUserNotificationCenter接收通知。但是也不行。

我尝试过这种方式,但我没有成功:

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

    let userInfo = response.notification.request.content.userInfo
    if let yourData = userInfo["yourKey"] as? String {
        // Handle your data here, pass it to a view controller etc.
    }
}

这是我在AppDelegate中的实现:

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

    let lNotification = UILocalNotification()
    lNotification.userInfo = response.notification.request.content.userInfo
        // Handle your data here, pass it to a view controller etc.

}

任何人,帮助我?我在这里看到了所有相关的问题,但没有发现任何问题。

1 个答案:

答案 0 :(得分:0)

您是否已注册通知? 如果没有,请将其添加到AppDelegate didFinishLaunchingWithOptions:

<h2>Labels</h2>
<code>$content.labels</code>
<h2>Children's Labels</h2>
<ul>
#foreach($child in $content.children)
    <li> <code> $child.labels </code> </li>
#end
</ul>
相关问题