–尝试向设备在Firebase数据库中拥有的所有用户发送通知,但在云函数的寄存器中显示:“ ReferenceError:未定义值”。
应该做的是:每次在“通知”中进行更改时,您都必须获取已更改的数据,并在设备表中查找此数据,并将通知发送到该设备中的所有令牌。
这是错误:
cloudfunction的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin'); // this is the Admin SDK, not firebase-functions
admin.initializeApp(functions.config().firebase);
const db = admin.database()
const ms = admin.messaging()
exports.notifications = functions.database.ref('Notifications/{id}').onUpdate(async(evt) =>{
const payload = {
notification:{
title : 'Alarma se activó tu cerca',
body : 'Se activó tu cerca, revisala',
badge : '1',
sound :'defaul'
}
};
//Get Notification Device ID
const notySnap = await db.ref('Notification/').once('value');
var devicee = notySnap.notty;
var dev = JSON.stringify(devicee);
//Get number of users that the device had
const usersSnap = await db.ref('Devices/'+ dev + '/Users').once(value)
const nUsers = usersSnap.nUsers;
var Nusers = JSON.stringify(nUsers);
var nNUsers = parseInt(Nusers);
//Send notification to the number of users that exist
if (Nusers !== null){
for(i = 1; 1 <=nNUsers; i++){
if(i === 1){
const userToSendP1 = usersSnap.user1;
var userToSend1 = JSON.stringify(userToSendP1);
Console.log("Mensaje enviado a user 1");
return ms.sendToDevice(userToSend1, payload);
}else if(i === 2){
const userToSendP2 = usersSnap.user2;
var userToSend2 = JSON.stringify(userToSendP2);
Console.log("Mensaje enviado a user 2");
return ms.sendToDevice(userToSend2, payload);
}else if(i === 2){
const userToSendP3 = usersSnap.user3;
var userToSend3 = JSON.stringify(userToSendP3);
Console.log("Mensaje enviado a user 3");
return ms.sendToDevice(userToSend3, payload);
}else if(i === 2){
const userToSendP4 = usersSnap.user4;
var userToSend4 = JSON.stringify(userToSendP4);
Console.log("Mensaje enviado a user 4");
return ms.sendToDevice(userToSend4, payload);
}
}
}
return null
})
数据库:
在此行:
const usersSnap = await db.ref('Devices/'+ dev + '/Users').once(value)
value
是您从未定义的变量。这就是错误消息的含义。您可能是用'value'
作为字符串来代替的:
const usersSnap = await db.ref('Devices/'+ dev + '/Users').once('value')