当前位置: 首页 > 知识库问答 >
问题:

Firebase云消息:未定义事件

幸经艺
2023-03-14

我在使用 Firebase 云消息通知时遇到问题。当我想发送好友请求时,其他客户端没有收到通知。Firebase Functions日志说:

ReferenceError: event is not defined
    at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:14:8)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:109:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
    at /var/tmp/worker/worker.js:728:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

以下是JavaScript代码:

'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((change,context) => {
 const receiver_id = context.params.receiver_id;

 const notification_id = context.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 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.');
    });
  });

});

共有1个答案

毛声
2023-03-14

更改此设置:

if (!event.data.val) {
return console.log('A notification has been deleted from database: ', notification_id);
 }

变成这样:

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

< code>change对象有两个属性< code>after和< code>before,每个属性都是< code>DataSnapshot,其方法与< code > admin . database . DataSnapshot 中的方法相同。

另外,val()是一个方法而不是属性。

 类似资料:
  • 按照Firebase开发文档中的说明,我已经实现了一个扩展Firebase MessagingService的服务,并覆盖了onMessageRec的回调。我已经将一条Log消息放在onMessageRec的方法的第一行中。 应用程序在后台运行我没有在logcat中看到日志,但我看到系统中发布了一个通知。 应用程序在前台我既没有看到日志也没有通知在系统托盘 知道这是怎么回事吗? 清单 服务等级

  • FCM服务未向我的iOS应用程序发送消息。 > App CAN成功接收APNs令牌和实例ID令牌 App CAN使用推送通知实用程序利用. p8令牌在后台成功接收来自APN的推送 #2中使用的相同APNs密钥上传到Firebase控制台 应用程序无法接收Firebase控制台中Notification Composer发送的消息,也无法使用CURL请求接收消息。 应用程序在通过FCM发送时不显示任

  • null 我遵循Xamarin的文档实现了这个功能。 然后一步一步地执行,直到下面的部分: 后台通知 我点击了Log Token按钮并收到了令牌。 null Firebase控制台显示消息为“完成”。 我错过了什么来解决这个问题?

  • 在我的Android应用程序中,我收到使用Firebase发送的消息,问题不是所有消息都到达,有时消息到达非常慢。 在我的服务器端,我跟踪我发送到FCM的消息,我总是收到成功:来自FCM的1个响应,仍然有我在Android应用程序中没有收到的消息。 我认为FCM消息日志在上面描述的情况下会有很大的帮助,但我不确定是否存在此选项。 有办法浏览Firebase消息日志吗?

  • 通知处理程序 邮递https://fcm.googleapis.com/fcm/send 当我发送通知与有效载荷上面,通知交付到系统托盘,当我点击它,我有以下三种情况: 应用程序处于后台(关闭/终止),显示“数据”并调用onLaunch方法。 问题是,如何接收或处理带有通知的“数据”也发送到系统托盘?任何帮助都将不胜感激。

  • null 当我确实从Firebase云消息发送给自己一条测试消息时,它确实工作正常,并且成功地向每个设备发送了一个推送通知。 例如,以下是 null