错误:"data must only contain string values" Firebase 云消息传递



我正在尝试通过FCM(Firebase Cloud Messaging(将一些数据从我的节点.js服务器发送到Android客户端。发送时出现以下错误:"数据只能包含字符串值"。我的数据包含 2 个 JSON 输出。我必须将它们转换为字符串还是要去这里?谢谢。

var message = {
notification:{
"title": "Alert!",
"body": position[0] + " has left Area: " + activeGeofences[i][1].name
},
data:{
Geofence: activeGeofences[i][1],
Position: position[1]
},
token: activeGeofences[i][0]
};

要将任何 JSON 对象转换为字符串,可以使用JSON.stringify().在接收端,您可以使用JSON.parse()(或平台的等效项(将字符串解析回树结构。

你也可以这样做,因为它对我来说似乎更准确

data: {
key1: "value1",
key2: "value2",
}

你只需要确保 value1 或 value2 或任何 n 键的值对是字符串,如果它是一个 int 或其他任何它会抛出错误的东西。这可以使您免于解析内容。

内部数据对象数据类型应始终为strings.
如果要传递数字数据类型,则会发生错误。

let message = {
notification: {
title: payload.title,
body: payload.message,
},
data: {
name: 'sds',
type: '2',
_id: 'sdsd',
},
token: deviceToken,
};

如果有人在Python上挣扎。显然它必须是一个只包含字符串的对象

body = json.dumps(data_object)
def send_notification(topic, title, body):
"""Send FCM notification to a specific topic"""
message = messaging.Message(
topic=topic,
data={"message": body},
notification=messaging.Notification(
title=title,
body=body
)
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

最新更新