【问题标题】:Device to device push notification firebase - onWrite issue设备到设备推送通知firebase - onWrite问题
【发布时间】:2019-09-13 07:13:36
【问题描述】:

我有一个 Android 应用程序,希望人们(经过身份验证的用户)通过消息框互相发送推送通知。我正在使用带有 firebase 云功能的 node.js,我在日志中得到了这个 error

TypeError:无法读取未定义的属性“userId” 在exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:10:33) ...

消息已成功写入实时数据库,但通知未传递给接收者(用户)。

我阅读了很多文档和相同/类似的问题,所以我知道有很多与此相关的主题,但我无法解决问题。我使用index.js作为源代码,但根据我阅读的文档更改了一些部分,如onWrite部分。

错误表示以下代码中的这一行: const receiverId = context.params.userId; params 出了问题。这是代码的一小部分(我的更改):

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/messages/{userId}/{messageId}').onWrite((change,context) => {

//get the userId of the person receiving the notification because we need to get their token
const receiverId = context.params.userId;
console.log("receiverId: ", receiverId);

//get the user id of the person who sent the message
const senderId = change.child('user_id').val();
console.log("senderId: ", senderId);

//get the message
const message = change.child('message').val();
console.log("message: ", message);

//get the message id. We'll be sending this in the payload
const messageId = context.params.messageId;
console.log("messageId: ", messageId);

...

【问题讨论】:

    标签: node.js firebase-realtime-database firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    获取最新版本的节点 并看到这个:https://firebase.google.com/docs/functions/database-events#handle_event_data 用于 onWrite(event...) 和 onCreate(.....) 对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 2020-12-24
      相关资源
      最近更新 更多