当iOS应用被暂停/终止并且用户单击通知时,如何处理Firebase推送通知?

时间:2019-09-20 23:16:20

标签: ios swift firebase push-notification

我想知道在应用程序被挂起/被杀死时应该回叫什么功能或如何收到通知。我可以在我的APP处于前台/后台时进行处理,但是当该应用程序被杀死时,我不确定该如何以及在何处处理或解析我的通知有效载荷?

1 个答案:

答案 0 :(得分:1)

从通知中打开应用程序时,您将在application: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.
    }
}

Swift 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.
    }
}