Firebase功能如何在Android中发送通知 [英] Firebase functions how to send a notification in Android

查看:106
本文介绍了Firebase功能如何在Android中发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次从firebase数据库中修改聊天时,都会激活该功能"sendNotification",但是会出现此错误:

I want that every time that chat is modified from the firebase database this function is activated "sendNotification" but this error appears:

sendNotification
 ReferenceError: receiverId is not defined
    at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:11:29)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
    at /var/tmp/worker/worker.js:730:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我有这个JavaScript代码,但我不知道为什么它告诉我没有定义receiveId,非常感谢.

I have this javascript code and I do not know why it tells me that receiverId is not defined, thank you very much.

let functions = require('firebase-functions');

let admin = require('firebase-admin');

admin.initializeApp();

exports.sendNotification = functions.database.ref('/Chat/{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 = context.data.child('user_id').val();
  console.log("senderId: ", senderId);

  //get the message
  const message = context.data.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);

  //query the users node and get the name of the user who sent the message
  return admin.database().ref("/users/" + senderId).once('value').then(snap => {
    const senderName = snap.child("name").val();
    console.log("senderName: ", senderName);

    //get the token of the user receiving the message
    return admin.database().ref("/users/" + receiverId).once('value').then(snap => {
      const token = snap.child("messaging_token").val();
      console.log("token: ", token);

      //we have everything we need
      //Build the message payload and send the message
      console.log("Construction the notification message.");
      const payload = {
        data: {
          data_type: "direct_message",
          title: "New Message from " + senderName,
          message: message,
          message_id: messageId,
        }
      };

      return admin.messaging().sendToDevice(token, payload)
        .then(function(response) {
          return console.log("Successfully sent message:", response);
        })
        .catch(function(error) {
          return console.log("Error sending message:", error);
        });
    });
  });
});

推荐答案

显然您正在使用Firebase SDK for Cloud Functions的旧版本,该版本为<到1.0版,但是您的语法(onWrite((change, context)))对应于> = 1.0版.

Apparently you are using an old version of the Firebase SDK for Cloud Functions, which is < to version 1.0, but your syntax (onWrite((change, context))) corresponds to version >= 1.0.

该图像显示onWrite.event上的错误,该错误与旧语法(.onWrite((event)))相对应.

The image shows an error on onWrite.event which corresponds to the old syntax (.onWrite((event))) .

您应该在项目中更新SDK,如下所示:

You should update the SDK in your project, as follows:

npm install firebase-functions@latest --save
npm install firebase-admin@latest --save-exact

您还应该将Firebase CLI更新到最新版本:

You should also update Firebase CLI to the latest version:

npm install -g firebase-tools

有关所有详细信息,请参见此文档项目.

See this documentation item for all the details.

这篇关于Firebase功能如何在Android中发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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