Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用firebase admin发送主题通知_Node.js_Firebase_Firebase Cloud Messaging_Backend - Fatal编程技术网

Node.js 使用firebase admin发送主题通知

Node.js 使用firebase admin发送主题通知,node.js,firebase,firebase-cloud-messaging,backend,Node.js,Firebase,Firebase Cloud Messaging,Backend,我需要通过fcm中的主题实现广播通知。我正在使用firebase管理员发送这些。任何人都可以发布代码片段,通过node.js发送这些通知吗?您需要 Firebase项目 设备注册令牌 火基云函数 firebase通知的工作: 设备到设备通知: 从设备1-firebase数据库(函数在写入数据库时触发)-firebase云函数(云函数将向开发人员2发送通知)-设备2首先,您需要向用户订阅给定主题 // These registration tokens come from the client F

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

您需要 Firebase项目 设备注册令牌 火基云函数

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

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

// 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);
  });
然后您可以使用

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

您可以阅读更多有关它的信息

已经阅读了他们的文档。我想知道我们应该在firebase admin中将服务器密钥传递到哪里。初始化应用程序时,我们应该在何处传递服务器密钥。哦,好的,我知道了,只需打开Firebase控制台,然后选择您的项目,然后选择其项目设置,然后选择服务帐户选项卡,复制初始化代码,并通过生成私钥下载服务帐户密钥。在初始化应用程序中,我们有admin.initializeApp({credential:admin.credential.applicationDefault(),})这是什么admin.credential.applicationDefault()。我们是否需要在applicationDefault()函数中传递serverKey.admin.credential.applicationDefault()呢返回从Google应用程序默认凭据创建的凭据,该凭据授予管理员对Firebase服务的访问权限。此凭据可用于调用admin.initializeApp()。Google应用程序默认凭据可用于任何Google基础架构,如Google App Engine和Google Compute Engine。
    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);
      });