在我的应用程序中,即使应用程序关闭,我也需要在没有用户交互的情况下访问推送通知有效负载。我在 didFinishLaunchingWithOptions
方法中使用了 launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
。但问题是,当通知到来时,有时 launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
包含有效负载,有时不包含有效负载。为什么?
我在didFinishLaunchingWithOptions
中的代码如下:
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil
{
UserDefaults.standard.set(launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification], forKey: "NotificationDetails")
UserDefaults.standard.synchronize()
handlePushNotificationUserInfo(userInfo: launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as! [AnyHashable : Any])
}
在上面的代码中,有时我会在 UserDefaults
中收到通知负载,有时则不会。
最佳答案
仅当用户点击通知时您才会收到通知。我把我用来通知的内容放在下面,没有收到任何问题。你可以查看我的代码。
if let launchOptions = launchOptions {
if let info = launchOptions[UIApplicationLaunchOptionsKey.remoteNotification] {
let dic = info as! NSDictionary
}
}
关于ios - 即使应用程序快速关闭,如何处理推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455894/