首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用firebase-admin发送主题通知

使用firebase-admin发送主题通知
EN

Stack Overflow用户
提问于 2021-01-15 16:42:37
回答 3查看 714关注 0票数 0

我需要在fcm中通过主题实现广播通知。我正在使用firebase-admin发送这些文件。任何人都可以发布通过node.js发送这些通知的代码片段吗?

EN

回答 3

Stack Overflow用户

发布于 2021-01-15 18:30:52

首先,您需要为用户订阅给定的主题

代码语言:javascript
复制
// These registration tokens come from the client FCM SDKs.
var registrationTokens = [
   'YOUR_REGISTRATION_TOKEN_1',
  // ...
   'YOUR_REGISTRATION_TOKEN_n'
];

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

,然后您可以使用广播通知

代码语言:javascript
复制
// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';

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

你可以阅读更多关于它的here

票数 1
EN

Stack Overflow用户

发布于 2021-01-15 18:19:33

您将需要Firebase项目设备注册令牌firebase云函数

firebase通知的工作原理:设备到设备通知:从设备1 -firebase数据库(函数在写入数据库时触发)-firebase云函数(云函数将向dev 2发送通知)- device2

票数 0
EN

Stack Overflow用户

发布于 2021-06-02 09:52:57

代码语言:javascript
复制
    const topic = "test";
    const payload = {
      notification: {
        title: "New news",
        body: "2:45",
      },
    };
    admin
      .messaging()
      .sendToTopic(topic, payload)
      .then((response2) => {
        // Response2 is a message ID string.
        console.log("Successfully sent message:", response2);
      })
      .catch((error) => {
        console.log("Error sending message:", error);
      });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65732931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档