gpt4 book ai didi

ios - 直接运行代码以响应接收到的 APNS 消息

转载 作者:行者123 更新时间:2023-11-28 23:25:51 27 4
gpt4 key购买 nike

我们有一个 iOS 应用程序,我们通过 FCM 向其发送通知。

理想情况下,我们希望能够让应用程序直接从通知运行一些代码。

如果我们发送数据类型通知并且应用程序在前台,这似乎是可行的。

但是 - 如果a) 应用程序处于后台b) 该应用程序已终止。有什么方法可以自动启动应用程序,或者让应用程序在终止时重新运行。

谢谢

最佳答案

应用在前台

在 AppDelegate 中实现 didReceiveRemoteNotification 委托(delegate)方法来处理通知并在应用程序处于前台时运行您想要的代码。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
//write your code here when app is in foreground
}

应用在后台

当应用程序在后台并且用户收到推送通知时,如果用户点击通知,didReceiveRemoteNotification 将被调用,但如果用户点击应用程序图标,则不会调用此方法,在此如果您必须与您的服务器通信,或者您可以使用以前的值检查角标(Badge)编号(您可以将其存储在 NSUserDefaults 中)并运行您想要的代码。

应用已终止

当应用程序终止时,didReceiveRemoteNotification 方法将不会被调用,但是您可以在 appdelegate 的 didFinishWithLaunchOptions 方法中检查 launchOptions 以了解应用程序是否已根据通知启动,并相应地执行您的任务。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
// Do your task here
}

}

关于ios - 直接运行代码以响应接收到的 APNS 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com