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云功能向它们发送通知?_Node.js_Firebase_Firebase Cloud Messaging_Google Cloud Functions - Fatal编程技术网

Node.js 如何从实时数据库中检索保存的设备令牌,并使用firebase云功能向它们发送通知?

Node.js 如何从实时数据库中检索保存的设备令牌,并使用firebase云功能向它们发送通知?,node.js,firebase,firebase-cloud-messaging,google-cloud-functions,Node.js,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我想向一小群设备发送通知,所以我想在firebase云功能中使用“SendToDevice”选项。我不熟悉javascript,所以请帮助我从firebase实时数据库检索令牌并向它们发送通知 存储在我的数据库中的设备令牌的结构,这些密钥是令牌: { "tokens" : { "-KdD1f0ecmVXHZ3H3abZ" : { "token" : "true", }, "-KdG4iHEYjInv7ljBhgG" : { "token"

我想向一小群设备发送通知,所以我想在firebase云功能中使用“SendToDevice”选项。我不熟悉javascript,所以请帮助我从firebase实时数据库检索令牌并向它们发送通知

存储在我的数据库中的设备令牌的结构,这些密钥是令牌:

{   
 "tokens" : {
    "-KdD1f0ecmVXHZ3H3abZ" : {
      "token" : "true",
    },
    "-KdG4iHEYjInv7ljBhgG" : {
      "token" : "true",
    }
}
错误:Firebase.child失败:第一个参数是无效路径: “/shopdata/${shopKey/${acYear}/notestokens/${sectionKey}”。路径必须 必须是非空字符串,并且不能包含“.”、“$”、“[”或“]”

我试图删除这些美元符号,但我再次收到一条日志消息:“没有要发送的通知令牌。”

这是我的代码:

exports.sendClassNotesNotification = functions.database.ref('/shopdata/{shopKey}/{year}/notes/{sectionKey}').onWrite(event => {

  const shopdata = event.params.shopdata;
  const year = event.params.year;
  const sectionKey = event.params.sectionKey;

  // Get the list of device notification tokens.

  const getDeviceTokensPromise = admin.database().ref('/shopdata/${shopKey/${year}/notestokens/${sectionKey}').once('value');

    return Promise.all([getDeviceTokensPromise]).then(results => {
    const tokensSnapshot = results[0];

    // Check if there are any device tokens.
    if (!tokensSnapshot.hasChildren()) {
      return console.log('There are no notification tokens to send to.');
    }
    const payload = {
      notification: {
        title: 'Section Note',
        body: `You have new note`
      }
    };

    // Listing all tokens.
    const tokens = Object.keys(tokensSnapshot.val());

    // Send notifications to all tokens.
    return admin.messaging().sendToDevice(tokens, payload);
  });
});

这样做,记住你必须从.once('value')返回承诺

对于工作代码示例,请参阅官方示例库

exports.pushNotification = functions.database.ref('/groupchat/{pushId}').onWrite( event => {
    return admin.database().ref('/tokens/...refrence...').once('value')
                    .then(function(tokensSnapshot){
        const tokens = Object.keys(tokensSnapshot.val());

        const payload = {
            notification: {
                title: 'App Name',
                body: "New Message",
                sound: "default"
            }   
        };

        return admin.messaging().sendToDevice(tokens, payload);
    });
});