【发布时间】: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