gpt4 book ai didi

node.js - FCM : getting ' Error: data must be a non-null object' error?

转载 作者:行者123 更新时间:2023-12-03 12:14:21 30 4
gpt4 key购买 nike

我正在尝试通过 Firebase 云消息发送推送通知。我正在使用 Firebase admin sdk 在 fcm 中发送推送通知。我正在使用 nodejs
当我尝试发送推送消息时,...
我收到此错误

{ 
code: 'messaging/invalid-payload',
message: 'data must be a non-null object' },
codePrefix: 'messaging'
}
我的代码:
const admin = require('firebase-admin');

const serviceAccount = require(`${__dirname}/fet_firebase.json`);

function sendPushNot(to, body, sendId, type) {

const registrationToken = to;
const notification = {};

let message = { };
const pbody = { body };

if (type === 'User') {
pbody.userId = sendId;
notification.userId = sendId;
notification.title = 'New user Follwed';
}
if (type === 'Post') {
pbody.postId = sendId;
notification.postId = sendId;
notification.title = 'Post Liked';
}
if (type === 'Room') {
pbody.roomId = sendId;
notification.roomId = sendId;
notification.title = 'New Chat messsage';
}

message = {
data: JSON.stringify(pbody),
token: registrationToken,
notification
};

console.log('messgae',message);

admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent cloud message:', response);
})
.catch((error) => {
console.log('Error sending cloud message:', error);
});
}
我以为 body 是空的
但是 console.log('messgae',message);的控制台输出是::
{
data:
'{"body":"pankaj Liked Your Post","postId":"5ed1055ddf0efd2a42f6a28a"}',
token:
'f2umP-jfQyeM1suN77zz7-:APA91bHKzfqfRnmuBom2PIDB8cCPwZtq28JCWLSi1OMPO55JRzyhFZJpTkkNyDu_StTYID-scu-grejaxxn3d4iR6Xidz9-JCk_h-bRsdGHe8nzMrIVsc8vZDFgayiFgJrJ53DaDzb9b',
notification: { postId: 5ed1055ddf0efd2a42f6a28a, title: 'Post Liked'
}
}
所以 body 不为空
但我收到 data must be a non-null object' error ..
为什么?

最佳答案

我通过用花括号包裹字符串化对象来解决这个问题

data : { data: JSON.stringify(object) } // correct
data : JSON.stringify(object) // will result to the described error.

关于node.js - FCM : getting ' Error: data must be a non-null object' error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62532849/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com