Title: Firebase cloud function sending multiple notification on instanceId
Tags: node.js;firebase;firebase-cloud-messaging;google-cloud-functions
Question: Thats my code where i first get the laterrides. It checks that the any ride is schedule and if any ride is scheduled then it goes inside and get the list of drivers and sends them notification. But the issue is it send notification repeatedly again and again.Kindly help.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.testing = functions.https.onRequest((req, res) =&gt; {
console.log("start");
var newArray = [];
let date = new Date();
var hours = date.getHours() +  5;
var minute = date.getMinutes() ;
var year = date.getFullYear() ;
var month = date.getUTCMonth() + 1 ;
var day = date.getDate() ;
var total_date = day + "/" + month + "/" + year;
var total_time = hours + ":" + minute;
var ridelater = admin.database().ref("ridelater");
ridelater.orderByValue().on("value", function(snapshot) {
snapshot.forEach(function(data) {
var str  = data.val().time;
const senderUid = data.val().client_key;
if(data.val().date == total_date){
var arr = str.split(":");
var start_minute = arr[0] * 60;
var val = parseInt(arr[1], 10);
var timing = val - 30;
var start = start_minute + timing ;
var end_minute = hours *60;
var end = end_minute + minute;
var diff = end - start;
if  (diff <= 30){
var driver = admin.database().ref("user/driver");
driver.orderByValue().on("value", function(snapshot) {
snapshot.forEach(function(instnaceIds) {
//if(instnaceIds.val().status == "free"){
const promises = [];
const receiverUid = instnaceIds.key;
const getInstanceIdPromise =
admin.database().ref(`/user/driver/${receiverUid}/instanceId`)
.once('value');
const getSenderUidPromise = admin.auth().getUser(senderUid);
Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results =&gt;
const instanceId = results[0].val();
//const sender = results[1];
console.log('notifying ' + receiverUid +' from ' + senderUid);
var data1={
'users_key': senderUid,
'org_address' : data.val().org_address,
'dest_address' : data.val().dest_address,
'status' : "request",
'Nodekey' : data.key,
'user_name' : data.val().user_name,
'org_lat' : data.val().org_lat,
'org_long' : data.val().org_long,
'dest_lat' : data.val().dest_lat,
'dest_long' : data.val().dest_long,
'payment' : data.val().payment_method,
'category' : data.val().category,
'Nodekey' : data.val().node_key,
};
var data2 = JSON.stringify(data1);
console.log(JSON.stringify(data1));
const payload = {
notification: {
title: "Qandeel Haider",
body: data2
}
};
console.log(payload);
return admin.messaging().sendToDevice(instanceId, payload)
.then(function (response) {
console.log("Successfully sent message:" + instanceId);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
//}
});
});
}
else{
console.log("Not found" + data.val().time);
else{
console.log("Not found" + data.val().date);
console.log("IN");
});
});
console.log("end");
res.status(200).end();
Comment: Do you got solution for that?

Comment: I am having same problem, my cloud function is sending a notification every couple of mins. Though it should send notification only once, for every time it gets trigger from db updates. But I am getting notification even when I have not updated db. The logs do not show that the cloud function got triggered, so how are notifications getting sent?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:07/20/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<node.js><firebase><firebase-cloud-messaging><google-cloud-functions>', 'Title': 'Firebase cloud function sending multiple notification on instanceId', 'ViewCount': 324, 'id': 45214617, 'user_AboutMe': '<p>Mobile Developer with around 5 years of experience in Native Android, and Cross Platforms like Flutter. I spent my career leading client-side Android development both on Java and Kotlin for small startups in Pakistan. I’ve contracted on multiple Android applications ranging from payment methods, real-time communication, food tracking, online ordering, restaurant management system and much more. Highly competent and skilled programmer. Highly innovative in designing.</p>\n', 'user_AccountId': 8358603.0, 'user_DisplayName': 'Qandeel Haider', 'user_Reputation': 67.0, 'user_Views': 20.0, 'user_id': 6277876.0}}