当应用程序通过通知启动时使用推送通知负载

Use Push Notification payload when App launches via the Notification

我想知道当用户通过打开通知启动应用程序时如何利用推送通知的有效负载。这可能吗?

我想实现一个在打开通知时发生的动作,但这个动作需要部分有效负载才能工作。

干杯

只需在您的 AppDelegate 中实现 application(_ application:, didReceiveRemoteNotification userInfo:)userInfo 是您的推送通知(包括有效负载)的 JSON 表示。

此外,您可以检查应用程序是否为“inactive”,这意味着(在本例中)用户刚刚通过此推送通知进入应用程序

示例代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if (UIApplication.shared.applicationState == .active) {
        // you just got a PushNotification, but the app is running currently
    } else if (UIApplication.shared.applicationState == .inactive) {
        // Do your work in here, the User just opened the App via the Push Notification
        // check userInfo for the Payload
    }
}