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

后端 未结 2 616
-上瘾入骨i
-上瘾入骨i 2021-02-08 19:15

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 \'n

相关标签:
2条回答
  • 2021-02-08 19:18

    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

    0 讨论(0)
  • 2021-02-08 19:41

    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));
    
    0 讨论(0)
提交回复
热议问题