【问题标题】:Functions notifications not working in chat app firebase功能通知在聊天应用程序 Firebase 中不起作用
【发布时间】:2020-04-28 13:51:29
【问题描述】:

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

这是 firebase 功能的日志 -

这里是 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/{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”

可能是什么问题?

【问题讨论】:

  • 您正在使用一个非常旧版本的 Cloud Functions for Firebase API,并且可能是一个非常旧的教程。请改用文档。 firebase.google.com/docs/functions

标签: java android firebase firebase-realtime-database


【解决方案1】:

改变这个:

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);
}

上下文参数提供有关函数执行的信息。跨异步函数类型相同,上下文包含字段 eventId、timestamp、eventType、resource 和 params。

查看指南:

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

【讨论】:

  • 它没有工作它现在在日志中给出了这个错误 - ReferenceError: event is not defined
  • 删除if(!event.data.val()) {并写if(!data.after.val()) {
  • 请阅读链接exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => { const beforeData = change.before.val(); // data before the write const afterData = change.after.val(); // data after the write });
  • 不,它没有用,我想它需要事件,因为每次我删除事件时,firebase 都会给我错误:未定义事件
猜你喜欢
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多