功能通知在聊天应用程序Firebase中不起作用 [英] Functions notifications not working in chat app firebase

查看:56
本文介绍了功能通知在聊天应用程序Firebase中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个android聊天应用程序,并使用firebase中的功能设置了自动通知,一切正常,但是当我发送消息通知时不会出现

I made a android chat app and setup the automated notifications using functions in firebase, everything is working fine but when i send a message notifications won't come

以下是Firebase功能中的日志-

Here are the logs in firebase function -

这是index.js文件-

Here is the index.js file -

'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(event => {
    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;

    if(!event.data.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 type = fromUserResult.val().type;

        const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
        const deviceToken = admin.database().ref(`/Users/${user_id}/token`).once('value');

        return Promise.all([userQuery, deviceToken]).then(result =>
        {
            const userName = result[0].val();
            const token_id = result[1].val();

            if(type == "request")
            {
                const payload =
                {
                    data:
                    {
                        title : "You have a Friend Request",
                        body: `${userName} wants to be your friend!`,
                        icon: "default",
                        click_action : "com.namandevloper.satyampublic_PROFILE_TARGET_NOTIFICATION",
                        from_user_id : from_user_id
                    }
                };

                return admin.messaging().sendToDevice(token_id, payload).then(response =>
                {
                    console.log(`${userName} (${from_user_id}) sent friend request to ${user_id}`);
                });
            }
            else if(type == "message")
            {
                const payload =
                {
                    data:
                    {
                        title : "You have a new Message",
                        body: `${userName} messaged you!`,
                        icon: "default",
                        click_action : "com.namandevloper.satyampublic_CHAT_TARGET_NOTIFICATION",
                        from_user_id : from_user_id
                    }
                };

                return admin.messaging().sendToDevice(token_id, payload).then(response =>
                {
                    console.log(`${userName} (${from_user_id}) send a message to ${user_id}`);
                });
            }
            else if(type == "accept")
            {
                const payload =
                {
                    data:
                    {
                        title : "You have a new friend",
                        body: `${userName} accepted your request!`,
                        icon: "default",
                        click_action : "com.namandevloper.satyampublic_PROFILE_TARGET_NOTIFICATION",
                        from_user_id : from_user_id
                    }
                };

                return admin.messaging().sendToDevice(token_id, payload).then(response =>
                {
                    console.log(`${userName} (${user_id}) accepted request by ${from_user_id}`);
                });
            }
        });
    });
});

登录firebase函数时说-无法读取未定义的属性'user_id'

Logs in the firebase Functions says - Cannot read property 'user_id' of undefined

可能是什么问题?

推荐答案

更改此内容:

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {
    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;
    if(!event.data.val())
    {
        return console.log('A Notification has been deleted from the database : ', notification_id);
    }

对此:

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;

if(!change.after.val())
{
return console.log('A Notification has been deleted from the database : ', notification_id);
}

context参数提供有关函数执行的信息.与异步函数类型相同,上下文包含字段eventId,timestamp,eventType,resource和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.

查看指南:

https://firebase.google.com/docs/functions/beta-v1-diff

这篇关于功能通知在聊天应用程序Firebase中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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