Reputation: 1256
I was trying to send multiple message to a topic from firebase cloud function with firebase admin sdk. But if the device is not connected to the network then I turn the network connectivity on I only receive the last message I sent inside onMessageReceived()
method on my android app. I want to receive all the message which was sent when device was not connected to the internet.
My cloud function code :
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.showNotification = functions.https.onCall((data,context) => {
var topic = 'weather';
var message = {
data: {
title: 'This is title',
description: getRandomString(15)
},
topic: topic,
android : {
ttl : 86400
}
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
console.log('Error sending message:', error);
});
});
Upvotes: 0
Views: 364
Reputation: 317437
Callable functions must return a promise from the top level of the function callback that resolves to the data to send to the client. Right now, your function is returning nothing, which means it terminates immediately and returns nothing. The return response
code is actually just returning a value from the then
callback function, not the top-level function. Try this instead, which should propagate that value out of the function and to the client.
return admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
console.log('Error sending message:', error);
});
It's very important to deal with promises correctly in functions code, otherwise they might not work at all.
Upvotes: 1