I have written a cloud function in Javascript that is supposed to send a notification if a change occurs in the database.
I have logged the values in the code and I am even getting a response in admin.messaging().sendToDevice(token, payload). And everything looks fine from the log in my Firebase Console, but I am not receiving any notification on my device. Also, the postman is set up correctly cause I do receive a notification when I press the Send button on the Postman.
This is my json body in Postman
{
"to" : "dCDqZxTYq3s:APA91bFlbFd3hGUJuvjknPhivRLew69kM4KDrNJAOkIMT5WgsoHr_Uc_41xqeOtQJvMhvXO1S56v4aT_6Zd24rlGoD-AV7pyNFMw8AxdkmwZCS3HYDidO2-xX_Da8IGcuQTN3FrnIYKo",
"data" : {
"message" : "This is my data message",
"title" : "This is my data title",
"data_type": "direct_message"
},
"notification" : {
"title": "This is a title",
"text": "this is a notification"
}
}
This is the index.js where my cloud function is written
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/problemDetails/{problemId}').onUpdate((change, context) => {
const problemId = context.params.problemId;
console.log("problemId: ", problemId);
//get the userId of the person receiving the notification because we need to get their token
const senderId = change.after.child('userId').val();
//get the status
const status = change.after.child('status').val();
// //get the user id of the person who sent the message
// const senderId = event.data.child('user_id').val(); //THis should be mechID
// console.log("senderId: ", senderId);
if(status === 'Price set by mechanic'){
//get the message
const message = "The mechanics has set the price for your service. Please check your current order if you have to take any actions.";
console.log("message: ", message);
//get the message id. We'll be sending this in the payload
const messageId = "messageId";
console.log("messageId: ", messageId);
//query the users node and get the name of the user who sent the message
return admin.database().ref("/users/" + senderId).once('value').then(snap => {
const senderName = snap.child("displayName").val();
console.log("senderName: ", senderName);
//get the token of the user receiving the message
return admin.database().ref("/users/" + senderId).once('value').then(snap => { //change senderId here to mechId who will be the receiver
const token = snap.child("messagingToken").val();
console.log("token: ", token);
//we have everything we need
//Build the message payload and send the message
console.log("Construction the notification message.");
const payload = {
data: {
data_type: "direct_message",
title: "New Message from " + senderName,
message: message,
message_id: messageId,
}
};
return admin.messaging().sendToDevice(token, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
});
}
else{
return null;
}
});