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

时间:2016-08-27 15:04:27

标签: php ios swift firebase google-cloud-messaging

我正在尝试使用一些自定义数据将有效负载通知从php发送到ios,当应用程序处于后台并收到通知时,该应用程序已打开但是它没有调用 didReceiveRemoteNotification 功能。仅当JSON的数据仅包含通知参数时才有效。

从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.")
}

1 个答案:

答案 0 :(得分:1)

经过大量测试后,我发现“内容可用”应该在通知中,并且它应该像“内容可用”,事实是firebase重新构造JSON以使其可读因此,您需要检查每次测试中他们向您发送数据的剂量。

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