Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用node.js的Firebase通知_Android_Node.js_Firebase_Firebase Realtime Database_Firebase Cloud Messaging - Fatal编程技术网

Android 使用node.js的Firebase通知

Android 使用node.js的Firebase通知,android,node.js,firebase,firebase-realtime-database,firebase-cloud-messaging,Android,Node.js,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,我正在使用node.js处理Firebase通知。 编译后,当我向应用程序的其他用户发送请求(请求发出通知)时,firebase日志显示错误: TypeError:无法读取未定义的属性“receiver\u id” 位于exports.sendNotification.functions.database.ref.onWrite.event(/user\u code/index.js:12:36) 反对。(/user_code/node_modules/firebase functions/li

我正在使用node.js处理Firebase通知。 编译后,当我向应用程序的其他用户发送请求(请求发出通知)时,firebase日志显示错误:

TypeError:无法读取未定义的属性“receiver\u id” 位于exports.sendNotification.functions.database.ref.onWrite.event(/user\u code/index.js:12:36) 反对。(/user_code/node_modules/firebase functions/lib/cloud functions.js:112:27) 在下一个(本地) at/user\u code/node\u modules/firebase functions/lib/cloud functions.js:28:71 at_uuwaiter(/user_code/node_modules/firebase functions/lib/cloud functions.js:24:12) 在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:82:36) at/var/tmp/worker/worker.js:700:26 在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)

Index.js代码:

'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的信息:

我想我必须根据上下文改变事件,但我不知道如何改变。 有人知道是什么问题吗? 感谢您的帮助:)

显示了
参数现在所在的位置:

context
参数提供有关函数执行的信息。在异步函数类型中相同,上下文包含字段
eventId
时间戳
eventType
资源
,以及
参数

因此,要消除该错误,您需要将函数的第一位更改为:

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;

        ...

您还需要进行更多类似的更改。如果您自己在制作这些代码时遇到困难,我建议您重新查看代码的来源。

好的,它会解决此错误并给我另一个错误。正如你所说的,我必须改变一些命令。不过,谢谢你的回复。这真的很有帮助。谢谢这个样品。奇怪的事。我已经点击了,但是我还没有足够的信誉点来显示这个操作。