直接响应收到APNS消息而运行代码

问题描述 投票:0回答:1

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

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

如果我们发送数据类型通知并且该应用位于前台,这似乎可行。

但是-如果a)该应用程序处于后台b)该应用程序已终止。是否有某种方法可以自动运行该应用程序,或者在终止时使该应用程序自行运行。

谢谢

ios apple-push-notifications voip ios-background-mode
1个回答
1
投票

[App位于前台

Implement didReceiveRemoteNotification AppDelegate中的委托方法,用于处理通知并在应用程序处于前台时运行所需的代码。

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

应用程序在后台

当应用程序处于后台并且用户收到推送通知时,如果用户点击通知,则会调用didReceiveRemoteNotification,但是如果用户点击应用程序图标,则不会调用此方法,在这种情况下,您必须与您的服务器通信,或者您可以使用先前的值检查徽章编号(可以将其存储在NSUserDefaults中)并运行所需的代码。

应用已终止

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

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.