【问题标题】:Not receiving notifications with firebase functions没有收到带有 firebase 功能的通知
【发布时间】:2019-01-28 15:35:02
【问题描述】:

我一直在尝试为我的应用程序实现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 代码。

index.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 函数日志猫

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)

【问题讨论】:

    标签: javascript java firebase firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    您使用的是 Cloud Functions for Firebase 测试版中的语法。由于它已更新到 1.0,因此语法发生了变化,您需要更新代码以匹配 upgrade documentation 中所述。

    将其应用于您的代码会导致如下结果:

    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);
    
    });
    

    变化是:

    • 有两个参数传递到函数中,以前只有一个。
    • 您不再需要将配置传递给initializeApp
    • beforeafter 快照现在可从 change 获得。

    还可以查看这些问题(searching for the error message 很容易找到:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2017-09-15
      • 2022-11-30
      相关资源
      最近更新 更多