问题描述
我正在使用node.js处理Firebase通知.编译后,当我向应用的其他用户发送请求时(请求发出通知),firebase日志显示错误:
I'm working with Firebase notifications using node.js.After compile, when I'm sending request to other user of app (request makes notification), firebase log shows error:
Index.js代码:
Index.js code:
'use strict'
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite(event =>
{
const receiver_id = event.params.receiver_id;
const notification_id = event.params.notification_id;
console.log('We have a notification to send to :', receiver_id);
if(!event.data.val())
{
return console.log('A notification has been deleted from the database: ', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');
return deviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "Friend Request",
body: "you have received a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was the notification feature.');
});
});
});
我已经在网站上阅读了有关新API的信息:
I have read about new APIs on site:
https://firebase.google.com/docs/functions/beta -v1-diff
我认为我必须将事件更改为上下文,但是我不知道如何.有人知道这是什么问题吗?感谢您的帮助:)
I think I must change event to context, but I don't know how.Is anybody know what's the issue?Thank's for any Help :)
推荐答案
新的data
和context
上的Firebase文档显示了params
现在所在的位置:
The Firebase documentation on the new data
and context
shows where the params
now live:
因此,要消除该错误,您需要将函数的第一位更改为:
So to get rid of that error, you'll need to change the first bit of your function to:
exports.sendNotification =
functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_id = context.params.receiver_id;
const notification_id = context.params.notification_id;
...
您还需要进行更多类似的更改.如果您在制作这些文件时遇到困难,建议您从提供代码的地方重新登录.
There are more, similar changes that you'll need to make. If you're having a hard time making those yourself, I recommend you check back in with where you got the code from.
这篇关于使用node.js的Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!