使用node.js的Firebase通知 [英] Firebase Notifications using node.js

查看:78
本文介绍了使用node.js的Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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:

TypeError:无法读取未定义的属性'receiver_id' 在exports.sendNotification.functions.database.ref.onWrite.event(/user_code/index.js:12:36) 在对象. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下(本机) 在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在__awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在/var/tmp/worker/worker.js:700:26 在process._tickDomainCallback(internal/process/next_tick.js:135:7)

TypeError: Cannot read property 'receiver_id' of undefined at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:12:36) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:700:26 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

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 :)

推荐答案

The Firebase documentation on the new data and context shows where the params now live:

context参数提供有关函数执行的信息.与异步函数类型相同,上下文包含字段eventIdtimestampeventTyperesource params .

The context parameter provides information about the function's execution. Identical across asynchronous functions types, context contains the fields eventId, timestamp, eventType, resource, and params.

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

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通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆