Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Firebase云消息在应用程序关闭时获取数据_Ios_Swift_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

iOS Firebase云消息在应用程序关闭时获取数据

iOS Firebase云消息在应用程序关闭时获取数据,ios,swift,firebase,push-notification,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" : "

我有一个应用程序。它使用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,type==“message”->转到MessageVC。我有一个问题,如果我的应用程序位于前台,我可以从
didReceiveMotonification:userinfo
获取数据,然后我可以检测推送屏幕,但是如果我的应用程序位于后台或关闭,我只会从
didReceiveMotonification:userinfo
获取没有数据的通知。当我点击通知时,它只会打开我的应用程序。非常感谢任何解决方案。

在Firebase iOS sdk中,您将在app delegate中获得以下代码片段

请注意,有两种userNotificationCenter方法。当应用程序处于前台时,将调用第一个应用程序。当您点击托盘上的推送通知时,第二个将被呼叫

完整代码可以从Firebase官方Github存储库(iOS Quickstart)中找到。

@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()
    }
}