How does one distinguish between Android and IOS Firebase IDs for Push Notifications?

Viewed 5229

As per my previously asked question, Firebase onMessageReceived not called when app is in the background , I need to change the payload to a 'data' payload as opposed to a 'notification' payload. (See link here -- What is the difference between Firebase push-notifications and FCM messages?).

The problem is, both the IOS and Android app we have utilize Firebase and the IOS app requires the push notification payload to use the 'notification' structure, while Android requires the 'data' payload structure.

My question is therefore, how do you distinguish between Android and IOS tokens / Ids obtained via the firebase sdk?

If our server saves these Ids and needs to send out a push notification, it needs to specify Android vs IOS in order to change the payload structure. Is the only way to accomplish this identification to have an app-based call to the server which differentiates IOS vs Android? Or is there a more sophisticated way using Firebase that will allow us to poinpoint which device it is?

Thanks all.

2 Answers

I faced the same issue, following is my approach to solve the issue.

Firebase supports "Topic messaging", in which we can send data or notification messages to multiple subscribed devices.

Lets consider user login email id is unique (Lets consider example email id is test@gmail.com), In android application user will subscribe to test_gmail.com_data topic (replace '@' with '_' in email id since topic name doesn't support '@') and in iOS application user will subscribe to test_gmail.com_notification topic, From cloud functions I am sending Data message which is intended to android device on data topic and Notification message which is intended to iOS devices on notification topic.

By this approach I solved the issue, only problem with this approach is we end up sending twice the same message.

Example Code :

 const data_message = {
          data: {
            "sender": "Narendra",
            "Message" : "Simple data message"
          },
          topic:"test_gmail.com_data"
        };
 const notification_message = {
          notification: {
            title: "Announcement"
          },
          data: {
            "sender": "Narendra",
            "Message" : "Simple data message"
          },
          topic: "test_gmail.com_notification"
        };
        promises.push(admin.messaging().send(data_message));
        promises.push(admin.messaging().send(notification_message));
Related