ios - 即使应用程序快速关闭,如何处理推送通知

标签 ios swift push-notification

在我的应用程序中,即使应用程序关闭,我也需要在没有用户交互的情况下访问推送通知有效负载。我在 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/

相关文章:

ios - 合并两个 NSArray 删除重复项

push-notification - PubNub 中的批量设备订阅

iOS 推送通知没有声音

ios - 当应用程序处于后台或终止时,FCM 通知 iOS 不工作

ios - "RCTBundleURLProvider.h"找不到文件 - AppDelegate.m

ios - 在前台和后台无限运行任务

ios - 以编程方式生成顶点

ios - TableView 复选框不接受索引 0 的操作

iOS 应用内购买收据,缺少最近的交易

swift - 两个枚举的单独常见情况 - Swift