【问题标题】:How to handle Firebase push notification when iOS app is suspended/killed and user clicks on the notification?当 iOS 应用程序被暂停/杀死并且用户点击通知时如何处理 Firebase 推送通知?
【发布时间】:2019-09-20 23:16:20
【问题描述】:

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

【问题讨论】:

    标签: ios swift firebase push-notification


    【解决方案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.
        }
    }
    

    斯威夫特 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.
        }
    }
    

    【讨论】:

    • 在 Swift 中怎么样?
    • 现在我需要展示和来自 appdelegate 的 Viewcontroller。我怎样才能做到这一点?
    • 您可以使用ApplicationDelegate.window.rootviewcontroller来呈现viewcontroller
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多