【问题标题】:Send Message To Group of Tokens in Firebase [duplicate]向 Firebase 中的令牌组发送消息 [重复]
【发布时间】:2019-01-13 21:57:30
【问题描述】:

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

【问题讨论】:

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


    【解决方案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);
      });
    
    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多