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.
With FCM, you can send two types of messages to clients: Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically. Data messages, which are handled by the client app.
It is important to note that a push notification must be sent to a device before you can send a notification to an individual user. All you have to do is specify the user's device token and app channel ID to the FCM server. If you want to know how to send push notifications to specific users. Contact WonderPush.
You can send notification messages using the Notifications composer in the Firebase console. Though this does not provide the same flexibility or scalability as sending messages with the Admin SDK or the HTTP and XMPP protocols, it can be very useful for testing or for highly targeted marketing and user engagement.
Ok, I finally managed to gather bits and pieces here and there to make it work. Don't understand why the docs do not have a clear and direct path to such a common requirement.
exports.sendComNotification = functions.firestore
.document('Comunicados/{comID}')
.onCreate((snap, context) => {
const doc = snap.data();
const msg = doc.title;
var message = {
notification: {
title: 'Comunicado da Diretoria!',
body: msg
},
data: {
title: 'Comunicado',
body: msg
},
android: {
notification: {
sound: 'default',
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
},
apns: {
payload: {
aps: {
badge: 1,
sound: 'default'
},
},
},
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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With