Javascript 不使用firebase函数接收通知

Javascript 不使用firebase函数接收通知,javascript,java,firebase,firebase-cloud-messaging,google-cloud-functions,Javascript,Java,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我一直在尝试为我的应用程序实现云消息,这样当一个新的孩子被添加到实时数据库时,应用程序的每个用户都会自动收到通知。 在myMainActivity中,我使用此方法为每个用户订阅一个主题 FirebaseMessaging.getInstance().subscribeToTopic("latest_events").addOnSuccessListener(new OnSuccessListener<Void>() { @Override

我一直在尝试为我的应用程序实现
云消息
,这样当一个新的孩子被添加到
实时数据库时,应用程序的每个用户都会自动收到通知。

在my
MainActivity
中,我使用此方法为每个用户订阅一个主题

FirebaseMessaging.getInstance().subscribeToTopic("latest_events").addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
               // Toast.makeText(MainActivity.this, "Successfully subscribed", Toast.LENGTH_SHORT).show();
            }
        });
当添加用户时,我没有收到所需的通知。似乎不明白我做错了什么

Firebase功能日志目录

sendNotification
TypeError: Cannot read property 'previous' of undefined at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:15:19) 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)

您正在使用Firebase云函数测试版的语法。由于它被更新为1.0,语法发生了变化,您将需要更新代码以匹配,如中所述

将其应用到代码中会导致如下结果:

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

exports.sendNotification = functions.database.ref("/Users").onWrite((change, context) => {

    var payload = {
        notification: {
            title: "A new user has been added!",
            body: "Click to see"

        }
    };

    if (change.before.exists()) {
        if (change.before.numChildren() < change.after.numChildren()) {
            return admin.messaging().sendToTopic("latest_events", payload);
        } else {
            return;
        }
    }

    if (!change.after.exists()) {
        return;
    }

    return admin.messaging().sendToTopic("latest_events", payload);

});
var functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp();
exports.sendNotification=functions.database.ref(“/Users”).onWrite((更改,上下文)=>{
var有效载荷={
通知:{
标题:“已添加新用户!”,
正文:“单击以查看”
}
};
if(change.before.exists()){
if(change.before.numChildren()
这些变化是:

  • 有两个参数传递到函数中,以前只有一个参数
  • 您不再需要将配置传递到
    initializeApp
  • 现在可以从
    change
    获得
    之前的
    之后的
    快照
另请参见以下问题(通过以下方式很容易找到):

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

exports.sendNotification = functions.database.ref("/Users").onWrite((change, context) => {

    var payload = {
        notification: {
            title: "A new user has been added!",
            body: "Click to see"

        }
    };

    if (change.before.exists()) {
        if (change.before.numChildren() < change.after.numChildren()) {
            return admin.messaging().sendToTopic("latest_events", payload);
        } else {
            return;
        }
    }

    if (!change.after.exists()) {
        return;
    }

    return admin.messaging().sendToTopic("latest_events", payload);

});