未收到 Firebase 功能的通知

我一直在尝试Cloud Messaging为我的应用程序实施,以便应用程序的每个用户在添加新孩子时都会自动收到通知Realtime Database.


在我的文章中,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 functions为我的后端安装并部署了我的 javascript 代码。


索引.js


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

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

admin.initializeApp(functions.config().firebase);


exports.sendNotification = functions.database.ref("/Users").onWrite(event => {


    var payload = {

        notification: {

            title: "A new user has been added!",

            body: "Click to see"


        }

    };


    if (event.data.previous.exists()) {

        if (event.data.previous.numChildren() < event.data.numChildren()) {

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

        } else {

            return;

        }

    }


    if (!event.data.exists()) {

        return;

    }


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


});

添加用户时,我没有收到所需的通知。似乎无法理解我做错了什么。


Firebase 函数日志 Cat


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)


蓝山帝景
浏览 153回答 1
1回答

长风秋雁

您使用的是 Cloud Functions for Firebase 测试版中的语法。由于它已更新到 1.0,因此语法发生了变化,您需要更新代码以匹配升级文档中所述。将其应用于您的代码会导致如下结果:var functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp();exports.sendNotification = functions.database.ref("/Users").onWrite((change, context) => {&nbsp; &nbsp; var payload = {&nbsp; &nbsp; &nbsp; &nbsp; notification: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: "A new user has been added!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body: "Click to see"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; if (change.before.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; if (change.before.numChildren() < change.after.numChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return admin.messaging().sendToTopic("latest_events", payload);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!change.after.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; return admin.messaging().sendToTopic("latest_events", payload);});变化是:有两个参数传递到函数中,而以前只有一个。您不再需要将配置传递到initializeApp.在before和after快照现在可从change。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java