【发布时间】:2018-12-03 12:08:59
【问题描述】:
这是我第一次使用 Firebase。我正在将 Firebase Cloud Message 与 node.js 服务器和 Android 客户端集成。当我将消息推送到服务器时,响应是消息成功发送到了 firebase 服务器。
但是,当我转到 firebase 控制台时,我看不到我的消息,而且我的 android 设备也没有收到该消息。我将消息作为主题发送,并且 android 设备也订阅了这些主题。
如果有人可以分享链接以查看服务器上的消息或建议我错过了什么,我会很高兴。
以下是示例响应: 将消息发送到 Firebase 以进行传递,响应: { “名称”:“项目/我的项目 ID/消息/7660010658785245660” }
下面是我用来推送到服务器的代码
let admin = require('firebase-admin'),
serviceAccount = require('the link to my service account is here'),
fcmObj = {};
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
fcmObj.sendMsg = function(){
let options = {
priority: "high",
timeToLive: 60 * 60 *24
},
message = {
data: {
content: 'Hello.. we are testing our api and fcm.',
sender: 'From Server'
},
topic: "News"
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message, options)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', JSON.stringify(response));
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
module.exports = fcmObj;
【问题讨论】:
-
通过 Firebase Cloud Messaging API 发送的消息不会显示在 Firebase 通知控制台中。因此,您在那里看不到它们的事实正在按预期工作。很难说为什么您通过 API 发送的消息没有显示在设备上,但是没有看到代码和更多详细信息。您可能想尝试从通知控制台发送消息,以查看这些消息是否到达。如果他们这样做,您至少知道您的 Android 应用程序是正确的。
-
当消息从通知控制台发送时,它会到达android客户端。
-
好的。所以问题在于你如何发送消息。请编辑您的问题以包含您用于发送消息的代码。了解您如何在 Android 上处理消息,尤其是您的测试方式可能也会有所帮助。
-
请查看我用来推送消息的 node.js 代码。我已经编辑了问题
-
所以你正在发送一条数据消息,这与控制台发送的通知消息完全不同。见the documentation on the messages types。你的 Android 代码中有
onMessageReceived吗?
标签: android node.js firebase firebase-cloud-messaging