Khaled Hasania Khaled Hasania - 3 months ago 60
Swift Question

Firebase notification data not callback didReceiveRemoteNotification when app in background

I'm trying to send payload notifications from php to ios with some custom data, when the app is in background and received a notification then tab on that notification the app is open but it's not calling the didReceiveRemoteNotification function. it's work only if JSON has data only without the notification param.

JSON that is sent from PHP to firebasr API

{
"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 Code

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

Answer

After a lot of testing i found that "content available" should be inside the notification and it should be like this "content-available", the thing is that firebase re-structure the JSON in a way to make it readable for ios so you need to check how dose they send you the data in each test.

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