我正在使用 firebase 发送多个推送通知。我的应用程序收到所有通知。现在,当应用程序被终止或终止时,如何将所有推送通知数据存储在用户默认值中? 当应用程序被终止并且用户从所有通知中单击一个推送通知时,如何存储所有推送通知数据?
最佳答案
您的应用程序不会仅通过推送通知来调用。您在通知托盘中收到的内容由 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.
}
}
}
关于iOS推送通知: how to store push notification payload when application is kill or terminate in swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600850/