'Firebase Functions "subscribeToTopic" works only for the first time

For some reason, I can't subscribe users for more than one topic

Here is my code:

exports.subscribe = functions.https.onCall((request, response) => {
  console.log(request);
  admin
    .messaging()
    .subscribeToTopic(request.token, request.topic)
    .then((response) => {
      console.log("Successfully subscribed to topic:", response);
    })
    .catch((error) => {
      console.log("Error subscribing to topic:", error);
    });
});

For the first time, everything works fine, but on the second time, I get some error in response:

 errors: [ { index: 0, error: [FirebaseMessagingError] } ]

So here are two questions:

  1. How to get this error text?
  2. How to subscribe user for multiple topics?

enter image description here

UPDATE: After additional investigation, I found that a subscription to a topic gets broken after I send a notification to this topic, only if a user who subscribed to this topic triggers that action..

I tried to do following: If was subscribed => unsubscribe => send push notification => subscribe back

It solved the problem, but I have a chat app - users suppose to send notifications all the time. I'm not sure that this is a suitable solution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source