我正致力于使用Firebase功能在Android设备之间发送通知。当A-Device向B-Device发送消息时,该消息将存储在A-Device Id和B-Device Id下的firebasedatabase中,如此问题是我写的功能总是检查A-Device因此,它始终将通知发送给A设备。这意味着当A-Device发送给B-Device时,通知将被发送到A-Device 这是我的Node.js代码。请帮助我;(
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//const mId = firebase.auth().currentUser.getToken();
exports.sendNotification = functions.database.ref('/Messages/{mId}/{uId}/{messageId}').onWrite(event =>{
const mId = event.params.mId; //getting mId
const uId = event.params.uId; //getting uId
const val = event.data.val(); //getting the values from event
const from = val.from; //from value
const type = val.type; //type value
const message = val.message;
console.log("user_name ",user_name);
console.log("user_email ",user_email);
const getDeviceToken = admin.database().ref(`/Users/${uId}/`).once('value');//getting device token function
return getDeviceToken.then(snapshot =>{ //executing the function
const DeviceToken = snapshot.val().DeviceToken;
console.log ("Device Token is ",DeviceToken);
if (from == mId) {
delete DeviceToken;
}
const getName = admin.database().ref(`/Users/${from}/`).once('value');//getting device token function
return getName.then(snapshot =>{ //executing the function
const Name = snapshot.val().Name;
console.log ("Sender is ",Name);
const payload = {
data: {
title: `${Name} sent you a message.`,
body: `${message}`
}
};
return admin.messaging().sendToDevice(DeviceToken,payload).then(response => {
console.log (`Notification has been sent to ${Name}`);
});});
});
});