I tried with the following function (node.js v8):
exports.sendComNotification = functions.firestore
.document('Comunicados/{comID}')
.onUpdate((snap, context) => {
console.log('Com triggered');
const newValue = snap.after.data();
const msg = newValue.title;
var message = {
notification: {
title: 'Comunicado da Diretoria!',
body: msg,
badge: '1',
sound: 'default',
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
topic: "Comunicados"
};
return admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
return
});
});
But that gives the following error in the fucntions log:
Error sending message: {
Error: Invalid JSON payload received.Unknown name "badge"
at 'message.notification': Cannot find field.
Invalid JSON payload received.Unknown name "sound"
at 'message.notification': Cannot find field.
Invalid JSON payload received.Unknown name "click_action"
at 'message.notification': Cannot find field.
at FirebaseMessagingError.FirebaseError[as constructor](/srv/node_modules / firebase - admin / lib / utils / error.js: 42: 28)
If I take out "badge", "sound" and "click_action", it works, but then there's no sound upon receival and the actions defined in onResume (flutter app) don't fire either, of course. What would be the correct way of setting the sound and click_action props? Thanks in advance.
badge
,sound
andclick_action
are legacy API fields. You probably need to use them with a legacy API method likeadmin.messaging().sendToTopic()
. See firebase.google.com/docs/cloud-messaging/… – Sonorantbadge
,click_action
etc there. – Sonorant