In my android application, I want to send notifications for text messages send from one user to another and I've deployed this Node.js function into firebase functions:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref(`/notification/{receiver_user_id}/{notification_id}`)
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const DeviceToken = admin.database().ref(`/users/${receiver_user_id}/user_id`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
console.log("Token Id ", token_id);
const payload =
{
notification:
{
title: "New Mesaage",
body: `you have a new Message, Please Check.`,
icon: "default"
}
};
console.log("This is payload ", payload);
return admin.messaging().sendToDevice(token_id, payload).then()
.catch(function (error)
{
console.log("Error sending message: ", error); // here no return
});
});
});
I think the problem lies with sendToDevice() method as I'm not getting any notification and I don't want to send by device_token.
I want to send notifications to the device in which the user with a particular "user_id" is logged in This is my database model