11

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.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • You could just store it in a variable if user uses ios or android – Martin De Simone Jul 24 '17 at 17:28
  • White true, Martin, we need to account for multiple devices and if a user owns both and logs into both or shares the account with people, it becomes an issue of consistency. – PGMacDesign Jul 24 '17 at 17:51
  • In that case you could send it both ways, with notification and data structure – Martin De Simone Jul 24 '17 at 18:00
  • It is not very clean, but as an easy fix it works – Martin De Simone Jul 24 '17 at 18:00
  • 1
    Also a good idea, and we have tried it, the problem is that means ios users will get 1 push notification and ignore the other, but Android users get one which is parsed by Firebase and the other which is parsed by me and there isn't a way to only show one of them if the app is in the background. – PGMacDesign Jul 24 '17 at 18:07
  • 1
    You are right, this is a very good question, hope you find the answer – Martin De Simone Jul 24 '17 at 18:17
  • I have the same exact problem, but the solution below doesn't help as I have >50k tokens. Is there a way to get platform based segregation in bulk? – Nagendra Rao Aug 02 '18 at 03:58

2 Answers2

20

Information about an app instance is available from the Instance ID Service at this endpoint:

https://iid.googleapis.com/iid/info/IID_TOKEN

On success the call returns HTTP status 200 and a JSON object containing various status for the app instance including the platform:

returns ANDROID, IOS, or CHROME to indicate the device platform to which the token belongs

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • 1
    Spot on, Bob. Ty for the information. For anyone not sure about how to get this instanceId, this id is obtained when you first start making calls to the Firebase SDK upon the app being opened. In Android, the docs are here: https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId . – PGMacDesign Jul 24 '17 at 22:38
  • This solution only allows checking instances one by one based on instance ID, is there a way to segregate platform tokens in bulk? I have about 50k tokens, and have the same problem as mentioned here, but I don't think I can use the above solution as it would take 50k calls to instanceID API to gather each one's platform type before triggering a push notification. Are there any other ways of doing this? – Nagendra Rao Aug 02 '18 at 03:55
  • @Rao: Not that I know of. – Bob Snyder Aug 02 '18 at 05:10
0

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));
narendra kumar
  • 289
  • 1
  • 3
  • 9