在 Firebase 控制台上查看发送的消息

Ebe*_*ben 7 android node.js firebase firebase-cloud-messaging

这是我第一次使用firebase。我正在将 Firebase Cloud Message 与 node.js 服务器和 Android 客户端集成。当我将消息推送到服务器时,响应是消息成功发送到 firebase 服务器。

但是当我转到 firebase 控制台时,我在那里看不到我的消息,而且我的 android 设备也没有收到消息。我将消息作为主题发送,Android 设备也订阅了这些主题。

如果有人可以共享链接以查看服务器上的消息或建议我遗漏了什么,我会很高兴。

下面是一个示例响应: 消息发送到 Firebase 以进行传送,响应:{ "name": "projects/my-project-id/messages/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;
Run Code Online (Sandbox Code Playgroud)

Mos*_*man 4

以下是示例响应:消息发送到 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!"
    },
Run Code Online (Sandbox Code Playgroud)