I'm trying to send Push Notifications with Firebase Cloud Functions with platform specific configuration. I got following config from https://firebase.google.com/docs/cloud-messaging/send-message
var message = {
notification: {
title: '$GOOG up 1.43% on the day',
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
},
data: {
channel_id: threadId,
},
android: {
ttl: 3600 * 1000,
notification: {
icon: 'stock_ticker_update',
color: '#f45342',
},
},
apns: {
payload: {
aps: {
badge: 42,
},
},
},
};
but got error for admin.messaging().sendToDevice(deviceToken, message)
Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification"
Any idea what is wrong here? Or maybe some samples of proper configuration for iOS/Android platforms?
sendToDevice()
is a function that uses the legacy FCM HTTP endpoint. The legacy endpoint doesn't offer platform-specific fields. In order to get that functionality, you can use the new endpoint through the send()
function. You may need to update your version of the Admin SDK. You can see an example in the documentation here.
For the code you provided, for example, you would send a message like this:
let message = {
notification: {
title: '$GOOG up 1.43% on the day',
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
},
data: {
channel_id: threadId,
},
android: {
ttl: 3600 * 1000,
notification: {
icon: 'stock_ticker_update',
color: '#f45342',
},
},
apns: {
payload: {
aps: {
badge: 42,
},
},
},
token: deviceToken,
};
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Notice the device token is now in the message
object.