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

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

我正在尝试检索我在 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()
}

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

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

提前致谢!

最佳答案

我在 swift 方面是这样使用的:

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"
   }
}

关于ios - 如何使用 Firebase 读取推送通知中发送的自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498363/

相关文章:

ios - 更改 UIViewController 后 NSNotificationCenter 崩溃

ios - iOS崩溃追随者

iOS - 在 Swift 代码中使用 Cocoapods objective-C

ios - 如何使用 Core Data 的选项和 "None"选项创建 Eureka AlertRow?

ios - UICollectionView 不显示

objective-c - iPad 行为的 SplitViewController

swift - SKPhysicsBody 避免碰撞 Swift/SpriteKit

Firebase 仅使用其 ID 从子集合中获取文档

android - 使用 Firebase BaaS 通过 Android 登录 Facebook

javascript - 无法从 firebase 云函数返回日期?