【问题标题】:View sent message on Firebase console在 Firebase 控制台上查看发送的消息
【发布时间】: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


【解决方案1】:

以下是示例响应:消息发送到 Firebase 以进行交付,响应:{ "name": "projects/my-project-id/messages/7660010658785245660" }

表示您的 Firebase SDK 正在成功发送消息。 (即凭据配置正确且 SDK 已成功初始化。)

在您的代码中,您已经定义了数据(有效负载是可选的,请参阅此处的文档:https://firebase.google.com/docs/cloud-messaging/concept-options#notification-messages-with-optional-data-payload),以显示您需要添加通知对象的通知消息。

只需在您的 message 对象中添加以下对象,然后它应该可以工作:

"notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },

【讨论】:

  • 感谢您的帮助。那是我错过的东西。我终于让它工作了。
猜你喜欢
  • 2020-12-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 2016-09-19
  • 2019-06-13
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多