使用Google云端功能和Google Firebase推送通知
问题描述:
我有一个使用Firebase的android 客户端应用程序和和管理应用程序。每当用户在客户端应用程序中注册时,我都需要向管理员应用发送推送通知。我正在尝试使用Cloud Functions for Firebase
。我已经导出了该功能,并且我也可以在Firebase控制台上看到该功能。使用Google云端功能和Google 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);
});
这里是我的数据库结构的火力点:
如果我使用任何门户网站发送推送甚至FCM发送推送到管理应用程序的测试目的,我收到了推。但是这个云端功能并没有发送这个推送。有人能指导我什么是错的,我正在做。
编辑
如果我改变功能的以下内容,然后它的作品。但我仍然想知道为什么上述功能不起作用。
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);
});
答
在你的原代码,您有:
const regToken = event.params.regToken;
event.params.regToken
不返回regToken
它返回wildcard path segment的参考值的值。
您是否收到成功或错误响应? –
添加日志语句并在Firebase控制台的日志窗格中查找输出:'console.log('userId:',userId,'token:',regToken);' –
Firebase日志输出说什么?您可以在Firebase控制台的“功能 - >日志”下找到它。 –