将消息发送到Firebase中的令牌组

时间:2018-08-06 21:51:38

标签: android node.js firebase firebase-cloud-messaging google-cloud-functions

我已经创建了一个聊天应用程序,收到群组消息时需要发送通知,我可以将通知发送到一台设备,但是我不知道如何处理群组通知。 有什么主意吗?

1 个答案:

答案 0 :(得分:1)

您需要订阅这些令牌并向该主题发送通知。

订阅主题

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);
  });

现在您可以像这样发送到特定的组

// 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);
  });