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

后端 未结 2 614
-上瘾入骨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: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));
    

提交回复
热议问题