Reputation: 226
I want my Cloud Function to send a push notification to a specific topic, which works just fine. However, when I'm adding the "badge" field, the following error occurs:
Unknown name "badge" at 'message.notification': Cannot find field.
What am I supposed to do if I want to specify a badge number for iOS devices? The code I have looks like follows:
exports.onAddLog = functions.database.ref("/NodesLog/{id}").onCreate((change, context) => {
const payload = {
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
}
}
const promise : Promise<any> = admin.messaging().send(payload)
.catch(error => console.error("Error on send: " + error))
.then(sendSuccess => console.log("Notification send "))
.catch(() => console.error("What happened?"))
return promise;
})
Upvotes: 5
Views: 9238
Reputation: 11
const payload = {
token: "FCMToken",
notification: {
title: "title",
body: "body",
},
apns: {
payload: {
aps: {
badge: 5,
sound: "default",
},
},
},
};
Upvotes: 1
Reputation: 3610
The following payload worked for me on both Android and iOS:
payload = {
notification: {
"title": "Message Title",
"body": "Message Body",
"click_action": ".MainActivity",
"badge": "1",
},
data: {
"my_message_type": "foo",
"my_id": "bar",
},
};
Upvotes: 1
Reputation: 2351
The problem is that badge is iOS field and it should be in another place as Ali said it is in "aps" object. But what he didn't say is where aps should be.
for example this should be your message
{
"message":{
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
},
"apns": {
"payload": {
"aps": {
"badge": 5
}
}
}
}
}
I took the example from here
Upvotes: 16
Reputation: 18610
try to add aps
node containing badge
property with your payload
const payload = {
...
"aps":{
"alert":"test alert",
"badge":5,
"sound":"default"
}
}
Upvotes: 2