Firebase Cloud Functions在Create上获取实时数据库上的数据 [英] Firebase Cloud Functions get data on real time database onCreate

本文介绍了Firebase Cloud Functions在Create上获取实时数据库上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/messages/diyetisyen/{uname}/{msgid}/message')
.onCreate((snapshot, context) => {

let message = snapshot.val();
let uname = context.params.uname;
let root = snapshot.ref.root;
let token = root.child('/users/' + uname + '/token').ref.token;

let payload = {
  data: {
    custom_notification: JSON.stringify({
      body: message + '',
      title: 'aaaa'

    })
  }
};
let options = { priority: "high" };


return admin
  .messaging()
  .sendToDevice(token, payload, options);
  });

我无法在/users/{uname}/token->值上获得令牌.

i cant get token on /users/{uname}/token -> value.

错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组.在FirebaseMessagingError.FirebaseError [作为构造函数](/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)在FirebaseMessagingError.PrefixedFirebaseError [作为构造函数](/srv/node_modules/firebase-admin/lib/utils/error.js:88:28)在新的FirebaseMessagingError(/srv/node_modules/firebase-admin/lib/utils/error.js:253:16)在Messaging.validateRegistrationTokensType(/srv/node_modules/firebase-admin/lib/messaging/messaging.js:911:19)在Messaging.sendToDevice(/srv/node_modules/firebase-admin/lib/messaging/messaging.js:532:14)在exports.sendNotification.functions.database.ref.onCreate(/srv/index.js:28:5)在cloudFunctionNewSignature(/srv/node_modules/firebase-functions/lib/cloud-functions.js:120:23)在/worker/worker.js:825:24在在process._tickDomainCallback(internal/process/next_tick.js:229:7)

Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:253:16) at Messaging.validateRegistrationTokensType (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:911:19) at Messaging.sendToDevice (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:532:14) at exports.sendNotification.functions.database.ref.onCreate (/srv/index.js:28:5) at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:120:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

推荐答案

您不能通过这样做获得数据库节点的值

You cannot get the value of a database node by doing

let token = root.child('/users/' + uname + '/token').ref.token;

您需要使用 <

You need to query the database with the once() method of the Reference, which is asynchronous.

这意味着您必须按以下步骤修改代码:

It means that you have to modify your code as follows:

exports.sendNotification = functions.database.ref('/messages/diyetisyen/{uname}/{msgid}/message')
.onCreate((snapshot, context) => {

    let message = snapshot.val();
    let uname = context.params.uname;
    let root = snapshot.ref.root;
    let tokenRef = root.child('/users/' + uname + '/token').ref;

    let payload = {
       data: {
         custom_notification: JSON.stringify({
           body: message + '',
           title: 'aaaa'   
         })
       }
     };

     let options = { priority: "high" };

     return tokenRef.once('value')
     .then(dataSnapshot => {
       const data = dataSnapshot.val();
       const token = data.token;   //Here I make the assumption the token is at '/users/' + uname + '/token/token'. You may adapt it as required

       return admin
       .messaging()
       .sendToDevice(token, payload, options);
     }); 
});  

这篇关于Firebase Cloud Functions在Create上获取实时数据库上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆