当 iOS 应用程序暂停/终止且用户点击通知时如何处理 Firebase 推送通知?

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

我想知道应该回调什么函数,或者当我的应用程序被挂起/终止时我收到通知。当我的应用程序处于前台/后台时,我能够处理,但是当应用程序被终止时,我不确定如何以及在哪里处理或解析我的通知有效负载?

ios swift firebase push-notification
2个回答
3
投票

从通知打开应用程序时,您将在应用程序中获取通知数据:将调用didFinishedLaunchingWithOptions方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        // opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
        }
    }
    else
    {
        // opened app without a push notification.
    }
}

斯威夫特 5.1

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    if launchOptions != nil {
        // opened from a push notification when the app is closed
        let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
        if userInfo != nil {
            if let object = userInfo?["aps"] {
                print("userInfo->\(object)")
            }
        }
    } else {
        // opened app without a push notification.
    }
}

0
投票

您可以在应用程序被杀死时处理推送通知,请在您的

SceneDelegate

中使用以下方法
  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {       
    if let notificationResponse = connectionOptions.notificationResponse {
         // get data from notification 
         let data = notificationResponse.notification.request.content.userInfo
         // get specific key from that data
         guard let feature = data["feature"] else {
             return
         }
     }      
}
© www.soinside.com 2019 - 2024. All rights reserved.