4

I using FCM to send notification for Android and iOS apps via my php server.Just because the different payload need by different platform,now I facing a big problem.

Now my payload structure look like this

"registration_id":[all my firebase token here]
 //***here just for iOS
 "notification":{
    "title":"Match update",
    "body":"Arsenal goal in added time, score is now 3-0"
    "sound" : "default"
    "priority" : 'high'
    "content_available" = true
 }
 //***here I need this for Android
"data":{
   "title":"Match update",
   "body":"Arsenal goal in added time, score is now 3-0"
   "id": "my_id"
    //other data I need in android
}

So by sending the payload I actually get the notification in iOS and Android at the same time.Which is good,but..

The real problem start here..

In android,I dont have my app scan for the notification field,I want to handle my app when the notification arrive in Android device start from "data" field.Although I dont have get data in the "notification",the app seem automatically handle for me for the "notification" data.

But if I not include the "notification" field in the payload above,Android part is behave as what I want,but in iOS,I unable to get the notification at all.

After look at this Platform Override mention in this answer,I modify my payload like below:

"registration_id":[all my firebase token here]
"android":{
  "data":{
        "title":"Match update",
        "body":"Arsenal goal in added time, score is now 3-0"
    }
}
"apns":{
   "payload":{
            "aps":{
               "alert":{
                  "title":"Hello",
                  "body":"Please confirm your availability"
               }
            "sound" : "default"
            "priority" : 'high'
            "content_available" = true
            }
}

But with the payload structure above,both my Android and iOS cant receive any notification.

Note: Both my Android and iOS app can get notification from Firebase console.

So my question is : How to send different structure of payload to different platform to solve this kind of dilemma?

ken
  • 2,426
  • 5
  • 43
  • 98
  • make two calls ? – YvesLeBorg Mar 09 '18 at 17:24
  • @YvesLeBorg I cant,cause all ios and android fcm token is inside 1 same array – ken Mar 09 '18 at 17:26
  • `registration_id` is not a key for FCM v1. Currently, you can send to a single device using the `token` key, to a topic using the `topic` key, or to topic conditions using `condition`. See the [documentation](https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices) for more information and a complete list of [payload options](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages). – Jen Person Mar 09 '18 at 18:54
  • @JenPerson so in this case I need a for loop to call the firebase API 1 by 1 for all token inside the array?? – ken Mar 09 '18 at 18:56
  • That is one option. Or you can use topics. Or you can use a batch request, which uses notation similar to [this](https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch). We're working on incorporating batch notation into the FCM documentation. – Jen Person Mar 09 '18 at 20:47

1 Answers1

1

Please read the documentation about platform-specific messaging.

The sample message from that page looks like this:

{
  "message":{
     "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
     "notification":{
       "title":"Match update",
       "body":"Arsenal goal in added time, score is now 3-0"
     },
     "android":{
       "ttl":"86400s",
       "notification"{
         "click_action":"OPEN_ACTIVITY_1"
       }
     },
     "apns": {
       "headers": {
         "apns-priority": "5",
       },
       "payload": {
         "aps": {
           "category": "NEW_MESSAGE_CATEGORY"
         }
       }
     },
     "webpush":{
       "headers":{
         "TTL":"86400"
       }
     }
   }
 }

Your JSON doesn't seem to be structured like that at all. Try copying the sample structure and working from there, paying attention to the nesting of the various elements.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441