php - 当应用程序在后台时,Firebase 通知数据不回调 didReceiveRemoteNotification

标签 php ios swift firebase google-cloud-messaging

我正在尝试使用一些自定义数据将有效负载通知从 php 发送到 ios,当应用程序在后台并收到通知时,然后在该通知上的选项卡上应用程序已打开但它没有调用 didReceiveRemoteNotification功能。它只有在 JSON 有 data 而没有 notification 参数时才有效。

从 PHP 发送到 firebasr API 的 JSON

{
   "to":"[Token]",
   "priority":"high",
   "content_available":true,
   "notification":{
        "title":"TITLE",
        "body":"Body",
        "sound":"default",
        "badge":"1"
    },"data":{
         "type":"link",
         "image":"",
         "link":"",
         "itemId":"",
         "categoryId":"",
         "groupId":""
    }
} 

ios 代码

func application(application: UIApplication,   didReceiveRemoteNotification userInfo:[NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: 
(UIBackgroundFetchResult) -> Void){
  print("vvvvvvvvvvvvv=%@", userInfo)
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 8.0, *) {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
        application.registerForRemoteNotificationTypes(types)
    }
     FIRApp.configure()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),name:kFIRInstanceIDTokenRefreshNotification, object: nil)
}


func applicationDidBecomeActive(application: UIApplication) {
    connectToFcm()
}

func applicationDidEnterBackground(application: UIApplication) {
    FIRMessaging.messaging().disconnect()
    print("Disconnected from FCM.")
}

最佳答案

经过大量测试后,我发现“可用内容”应该在通知中并且应该像这样“可用内容”,问题是 firebase 以某种方式重新构建 JSON 以使其可读ios 所以你需要检查他们在每次测试中如何向你发送数据。

{
 "to":"[Token]",
 "priority":"high",
 "notification":{
    "title":"TITLE",
    "body":"Body",
    "sound":"default",
    "badge":"1",
    "content-available":true,
 },"data":{
      "type":"link",
      "image":"",
      "link":"",
      "itemId":"",
      "categoryId":"",
      "groupId":""
 }
} 

关于php - 当应用程序在后台时,Firebase 通知数据不回调 didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39182544/

相关文章:

ios - 如何检测 iOS View Controller 的当前方向?

ios - 如何使用 Eureka 创建自定义内联行?

php - mysql 查询问题似乎无法找出原因

php - 统计点击次数,然后通过php将值传给db

ios - 架构 i386 的 undefined symbol : "_OBJC_CLASS_$_Analytics"

ios - 仅从 firebase 加载更新的文档?

swift - 对 Swift 错误感到困惑 - 无法找到并重载接受提供的参数的 'init'

swift - Swift 中的错误 "Trailing closure passed to parameter of type ' NSManagedObjectContext' 不接受闭包

php - API 按城市和酒店搜索,查找我的数据库中存在的城市和酒店

php - 在 Zend 表单中放置一个提交按钮的图标