【发布时间】:2017-09-12 05:13:30
【问题描述】:
我有一个使用 Firebase 的 android Client Application 和 Admin Application。每当用户在客户端应用程序中注册时,我都需要向管理应用程序发送推送通知。我正在尝试使用Cloud Functions for Firebase。我已经导出了这个函数,我也可以在 firebase 控制台上看到它。
这是我的 index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}/{regToken}').onWrite(event => {
if (event.data.previous.exists()) {
return;
}
const userId = event.params.userId;
const regToken = event.params.regToken;
// Notification details.
const payload = {
notification: {
title: 'You have a new User.',
body: `${userId} is the id.`,
}
};
return admin.messaging().sendToDevice(regToken, payload);
});
这是我在 firebase 的数据库结构:
如果我使用任何在线门户发送推送甚至 FCM 发送推送到管理应用程序以进行测试,我正在接收推送。但是这个 Cloud Function 并没有发送推送。有人可以指导我我在做什么错。
编辑
如果我将函数更改为以下,那么它可以工作。但我仍然想知道为什么上述功能不起作用。
exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}').onWrite(event => {
if (event.data.previous.exists()) {
return;
}
const userId = event.params.userId;
var eventSnapshot = event.data;
const regToken = eventSnapshot.child("regToken").val();
Notification details.
const payload = {
notification: {
title: 'You have a new User.',
body: `${userId} is the id.`,
}
};
return admin.messaging().sendToDevice(regToken, payload);
});
【问题讨论】:
-
您收到的是成功响应还是错误响应?
-
添加日志语句并在 Firebase 控制台的日志窗格中查找输出:
console.log('userId:', userId, ' token: ', regToken); -
您的 Firebase 日志输出说明了什么?您可以在 Firebase 控制台的
Functions -> Logs下找到它 -
userId: 123456793 token: regToken这是日志,缺少regToken值 -
我猜,我以错误的方式访问 regToken 值。
标签: android firebase firebase-cloud-messaging google-cloud-functions