我试图在我的项目中使用firebase实现推送通知但是无法这样做.Below是我的index.js文件,我对javascript和nodejs知之甚少,那就是为什么不能弄清楚问题。
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}').onWrite((data,context) =>
{
const receiver_id = context.params.receiver_id;
const notification_id = context.params.notification_id;
console.log('We have new notification to send to : ', receiver_id);
/*if(!context.data.val()){
return console.log('A notification has been deleted from the databse : ', notification_id);
}*/
const deviceToken = admin.database().ref(`Users/${receiver_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "Friend Request",
body : "You've received a new Friend Request",
icon : "default"
}
};
return admin.messaging().sendToDevice(token_id,payload).then(response => {
console.log('This was the notification feature');
return true;
});
});
});
任何人都可以向我解释这段代码并帮助解决我的问题。
答案 0 :(得分:0)
每个设备都有不同的令牌。似乎您只为一个用户存储一个令牌。这就是您可以将通知仅发送到一台设备的原因。如果要将其发送到多个设备,则必须存储多个设备令牌,并将通知发送给所有这些设备。