我一直在尝试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)
长风秋雁
相关分类