iOS Firebase Cloud Messaging 在应用程序关闭时获取数据

标签 ios swift firebase push-notification firebase-cloud-messaging

我有一个应用程序。它使用 FCM 推送通知。消息的 json 看起来像:

    {   "to": "xxx",   "notification" :  {
            "body" : "Hi",
            "badge" : 1,
            "sound" : "default"
        },
        "data" :     {
            "id" : "xxx",
            "first_name" : "xxx",
            "last_name" : "xxx",
            "full_name" : "xxx",
            "primary_image" : "xxx",
            "matchid" : "xxx", 
            "type": "match"/"message"
        },
        "content_available": true,
        "priority": "high" 
}

我在数据中有一个“类型”来检测触摸我的通知时将启动哪个屏幕。如果 type == "match"-> 转到 MatchVC,然后键入 == "message"-> 转到 MessageVC。我有一个问题,如果我的应用程序在前台,我可以从 didReceiveRemoteNotification:userinfo 获取数据,然后我可以检测到推送屏幕,但是如果我的应用程序在后台或关闭,我只会收到通知而不会来自 didReceiveRemoteNotification:userinfo 的数据。当我点击通知时,它只会打开我的应用程序。感谢任何解决方案。

最佳答案

在 Firebase iOS sdk 中,您将在应用委托(delegate)中拥有以下代码片段。

注意有 2 个 userNotificationCenter 方法。当应用程序处于前台时,将调用第一个。当您点击托盘中的推送通知时,将调用 2nd。

完整代码可以从 Firebase 官方 Github 存储库(iOS 快速入门)中找到。 https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print("userInfo 1st")
        print(userInfo)

        let id = userInfo["id"]
        let firstName = userInfo["first_name"]

        print(id ?? "")
        print(firstName ?? "")

        // Change this to your preferred presentation option
        completionHandler([])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print("userInfo 2nd")
        print(userInfo)

        let id = userInfo["id"]
        let firstName = userInfo["first_name"]

        print(id ?? "")
        print(firstName ?? "")

        completionHandler()
    }
}

关于iOS Firebase Cloud Messaging 在应用程序关闭时获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40291584/

相关文章:

ios - subview 将触摸传递给其下方的 subview

javascript - Firebase Web 检索数据

javascript - Firebase:初始化由集合组成的树

ios - Adobe AIR NetworkInfo native 扩展不支持 AIR 模拟器?

ios - 无法在 Linux 环境中运行/构建 IOS。 with/tmp 文件夹在另一个文件系统上

ios - 为 tvOS 构建,但链接到为 iOS 构建的目标文件,用于体系结构 arm64

ios - UIKeyboardDidShow 触发太频繁?

ios - 无法在 Today Extension ios 中加载 TableView

firebase - 根据 Firebase 数据库条件创建 Firebase 存储安全规则

iphone - RestKit 序列化对象快捷方式