admin管理员组文章数量:1208155
I wrote a cloud function using Cloud Functions for Firebase that sends notifications to certain topics of Firebase Messaging. The final parts of the function define the payload to be sent, then sends it:
// javascript code in cloud functions
const payload = {
'notification': {
'title': `${toTitleCase(name)} just logged an event`,
'body': `${events[eventType]} for ${toTitleCase(petName)}`,
'data': {
'personSent': userSent
}
}
};
console.log(payload);
admin.messaging().sendToTopic(pet_Id, payload);
However, I'm getting the error log in my Firebase console:
Error: Messaging payload contains an invalid value for the "notification.data" property. Values must be strings.
When I logout the payload I confirmed it is all strings:
{ notification:
{ title: 'Turtle Dude just logged an event',
body: 'Walk for Beer',
data: { personSent: 'mfsP8U0qDdQL4rrrbXp6K0YsF423' } } }
However, when I send the same payload from my iPhone app (which I'm trying to avoid since that means I have to store the messaging private key on the client side) I am able to attach the extra data I want to send just fine:
// Swift code in iPhone app
let body: [String: Any] = ["to": "/topics/\(currentPet)",
"priority" : "high",
"notification" : [
"body" : "\(events[eventType]) for \(petsName.localizedCapitalized)",
"title" : "\(myName.localizedCapitalized) just logged an event",
"data" : ["personSent": myId]
]
]
How can I accomplish adding additional data in my cloud function like I do in my Swift code?
I wrote a cloud function using Cloud Functions for Firebase that sends notifications to certain topics of Firebase Messaging. The final parts of the function define the payload to be sent, then sends it:
// javascript code in cloud functions
const payload = {
'notification': {
'title': `${toTitleCase(name)} just logged an event`,
'body': `${events[eventType]} for ${toTitleCase(petName)}`,
'data': {
'personSent': userSent
}
}
};
console.log(payload);
admin.messaging().sendToTopic(pet_Id, payload);
However, I'm getting the error log in my Firebase console:
Error: Messaging payload contains an invalid value for the "notification.data" property. Values must be strings.
When I logout the payload I confirmed it is all strings:
{ notification:
{ title: 'Turtle Dude just logged an event',
body: 'Walk for Beer',
data: { personSent: 'mfsP8U0qDdQL4rrrbXp6K0YsF423' } } }
However, when I send the same payload from my iPhone app (which I'm trying to avoid since that means I have to store the messaging private key on the client side) I am able to attach the extra data I want to send just fine:
// Swift code in iPhone app
let body: [String: Any] = ["to": "/topics/\(currentPet)",
"priority" : "high",
"notification" : [
"body" : "\(events[eventType]) for \(petsName.localizedCapitalized)",
"title" : "\(myName.localizedCapitalized) just logged an event",
"data" : ["personSent": myId]
]
]
How can I accomplish adding additional data in my cloud function like I do in my Swift code?
Share Improve this question edited Dec 1, 2017 at 16:00 Grimthorr 6,9265 gold badges43 silver badges53 bronze badges asked Mar 29, 2017 at 20:28 MarksCodeMarksCode 8,58417 gold badges70 silver badges142 bronze badges 4 |3 Answers
Reset to default 15As others explained in the comments, the data
object should go inside payload
,
NOT inside notification
.
Try the following code:
// javascript code in cloud functions
const payload = {
'notification': {
'title': `${toTitleCase(name)} just logged an event`,
'body': `${events[eventType]} for ${toTitleCase(petName)}`,
},
// NOTE: The 'data' object is inside payload, not inside notification
'data': {
'personSent': userSent
}
};
console.log(payload);
admin.messaging().sendToTopic(pet_Id, payload);
Along with Diego's answer, I would like to point out a crucial detail for anyone who visits this message in the future... "Make sure to PARSE ANY ObjectId's to String when assigning it to a value for any key inside DATA" . This has pretty much been the most unfigurable problem while dealing with FCM. And was the prime factor in solving my problem.
Whoever might experience this error by now. Using node V14.7.0 on Windows and firebase-admin version ^9.6.0 seem to work perfectly, I had no issue. However, when deploying it on Bitnami, the error was thrown. To solve it, I had to parse the object someObject
properties to be assigned to 'data' to string:
//someObject.property_throwing the error should be parsed to string
data: someObject
本文标签: javascriptPayload error in Cloud Functions for Firebase MessagingStack Overflow
版权声明:本文标题:javascript - Payload error in Cloud Functions for Firebase Messaging - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738753661a2110546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data
up a level:{notification: {}, data: {}}
– Michael Bleigh Commented Mar 30, 2017 at 0:15data
I just mademyId
the value todata
instead of nesting it. Not really sure why it didn't work though. – MarksCode Commented Mar 30, 2017 at 0:19'data' : userSent
instead of nesting it and that works. Its fine since I'm only sending one piece of data but incase I want to send more than one I'll try Michael's suggestion. – MarksCode Commented Mar 30, 2017 at 3:14