首页 > 解决方案 > iOS推送通知:当应用程序被快速终止或终止时如何存储推送通知有效负载?

问题描述

我正在使用 firebase 发送多个推送通知。我的应用程序收到所有通知。现在如何在应用程序被杀死或终止时将所有推送通知数据存储在用户默认值中?当应用程序被终止并且用户从所有通知中单击一个推送通知时,如何存储所有推送通知数据?

标签: iosswift

解决方案


您的应用程序不会仅通过推送通知被调用。您在通知托盘中收到的内容由 iOS 直接处理,推送通知的内容,特别是通知正文的 aps 键内的内容用于填充您在通知托盘中看到的内容。

如果应用程序处于挂起或终止状态,则不会仅仅因为您收到推送通知而调用该应用程序。当然,除非您的 apns-push-type 在您的通知中设置为背景和 content-available = 1。电话会转到

application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

您可以根据应用程序要求处理内容的地方。

要检测应用程序是否从通知启动,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    if let dict = launchingOptions?[.remoteNotification]{
        //App launched from Notification 
        if let userInfo = dict as? [AnyHashable:Any]{
            //Do what your app requires with the notification using the userInfo.
        }
    }
}

推荐阅读