我正在尝试使用Firebase云消息传递发送推送通知。因此,我使用下面的脚本发送了一个Friend Request的通知,该通知运行得很好,然后我尝试发送一条消息通知,但不知何故此错误不断重复。
Error: Messaging payload contains an invalid value for the
"data.from_user_id" property. Values must be strings.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor]
(/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor]
(/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-
admin/lib/utils/error.js:241:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:782:27
at Array.forEach (native)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:779:32
at Array.forEach (native)
at Messaging.validateMessagingPayload (/user_code/node_modules/firebase-
admin/lib/messaging/messaging.js:772:21)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:500:37
下面是我在做什么
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref('/notifications/{user_id}/{notification_id}')
.onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification from : ', user_id);
if (!change.after.val())
{
return console.log('A Notification has been deleted from the database : ',
notification_id);
}
const fromUser =
admin.database().ref(`/notifications/${user_id}/${notification_id}`)
.once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
const req_type = fromUserResult.val().type;
if (req_type=="message") {
const userQuerymsg =
admin.database().ref(`AllUsers/${from_user_id}/name`).once('value');
const deviceTokenmsg =
admin.database().ref(`/AllUsers/${user_id}/device_token`).once('value');
return Promise.all([userQuerymsg, deviceTokenmsg]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title : "New message Received",
body: `${userName} has sent you request`,
icon: "default",
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>
{
console.log('This was the message notification Feature');
});
});
}
if (req_type=="request") {
const userQuery =
admin.database().ref(`AllUsers/${from_user_id}/name`).once('value');
const deviceToken =
admin.database().ref(`/AllUsers/${user_id}/device_token`).once('value');
return Promise.all([userQuery, deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title : "New Friend Request",
body: `${userName} has sent you request`,
icon: "default",
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>
{
console.log('This was the notification Feature');
});
});
}
console.log('You have new notification from : ', from_user_id);
});
});
我正在将消息和好友请求通知都发送到一个表中,因此我必须检查通知是消息还是请求的通知。