0

I have Created a Chat App and I need to Send Notification when group messages receives,i can send notification to one device but i don't know what to do with group notification. any idea?

1 Answers1

1

You need to subscribe these token and send notification to that topic.

Subscribe to a topic

var registrationTokens = []
db.collection('room').document({roomId}).get().then(result => {
    registrationTokens = result.data().usersTokens // get user tokens in that chat room
})

// Subscribe the devices corresponding to the registration tokens to the
// topic.
admin.messaging().subscribeToTopic(registrationTokens, topic)
  .then(function(response) {
    // See the MessagingTopicManagementResponse reference documentation
    // for the contents of response.
    console.log('Successfully subscribed to topic:', response);
  })
  .catch(function(error) {
    console.log('Error subscribing to topic:', error);
  });

Now you can send to specific group like this

// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';

// See documentation on defining a message payload.
var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });
JustKhit
  • 407
  • 3
  • 9