如何使用Firebase读取推送通知中发送的自定义数据

问题描述 投票:1回答:1

我正在尝试检索我在iOS应用程序(Swift)中使用Firebase在推送通知中发送的“数据”

这是我用来发送通知的代码:

{
   "to": "dWB537Nz1GA:APA91bHIjJ5....",
   "data":
   {
      "message": "Offer!",
      "mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
   },
   "notification":
   {
      "body": "Enter your message",
      "sound": "default"
   }
}

我只实现了一种从通知中读取数据的方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //checked where I can get custom data which I sent but no success
    let notification =  response.notification.request.content.body

    print("Notification recived content:\(response.notification.request.content)")
    print("userNotificationCenter : didReceive response body : \(notification)")
    print("userNotificationCenter : didReceive response user info : \(response.notification.request.content.userInfo)")
    print("userNotificationCenter : response : \(response)")


    completionHandler()
}

检查上面的方法,我可以获得我发送的自定义数据,但没有成功

我在很多地方寻找解决方案但没有成功。请帮我检索自定义数据。

先感谢您!

ios swift firebase push-notification firebase-cloud-messaging
1个回答
0
投票

我在迅速的方面使用这样的:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


    if let menu = userInfo["Menu"] as? NSString {
        print(menu)
    }

    Messaging.messaging().appDidReceiveMessage(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)
}

这是我用来发送通知的代码:

{
   "to": "/topics/....",
   "data":
   {
      "Menu": "Summary"
   },
   "notification":
   {
      "title": "Header",
      "text": "Message",
      "sound": "default"
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.