flutterfirebase-cloud-messaging

FCM unsubscribe specific token From Topic


I'm facing now issue that , I have FCM topic and this topic has many subscriber I want the admin removes user from topic not this user unsubscribe by him self.


Solution

  • You can use firebase-admin. You just need to get the users' FCM tokens into an array then pass in the array of user tokens you would like to

    // These registration tokens come from the client FCM SDKs.
    var registrationTokens = [
      'YOUR_REGISTRATION_TOKEN_1',
      // ...
      'YOUR_REGISTRATION_TOKEN_n'
    ];
    
    // Unsubscribe the devices corresponding to the registration tokens from
    // the topic.
    admin.messaging().unsubscribeFromTopic(registrationTokens, topic)
      .then(function(response) {
        // See the MessagingTopicManagementResponse reference documentation
        // for the contents of response.
        console.log('Successfully unsubscribed from topic:', response);
      })
      .catch(function(error) {
        console.log('Error unsubscribing from topic:', error);
      });
    

    where the registrationTokens are the user tokens and topic is just String of the topic you would want to unscribe the user from.

    check out firebase documentation below on the topic