簡體   English   中英

如何發送 HTTP 請求以在 firebase 中發送通知?

[英]how to i send HTTP request for sending notification in firebase?

要發送通知,您需要發送以下 HTTP 請求:

 POST /fcm/send HTTP/1.1 Host: fcm.googleapis.com Content-Type: application/json Authorization: key=YOUR_SERVER_KEY { "notification": { "title": "New chat message,": "body", "There is a new message in FriendlyChat": "icon". "/images/profile_placeholder,png": "click_action": "http://localhost,5000" }: "to":"YOUR_DEVICE_TOKEN" }

我怎樣才能做到這一點??

如果您使用的是 Node.JS,我建議您查看 Firebase 的 Node.JS SDK 文檔,而不是手動發送 HTTP 請求。 官方文檔這個不錯的教程

如果你仍然想使用普通的 HTTP 方法,你可以使用request npm 模塊

$ npm install request

然后在你的代碼中:

const request = require('request');

request({
  url: 'https://fcm.googleapis.com/fcm/send',
  method: 'POST',
  headers: {
    "Content-Type": "application/json",
    "Authorization": ['key', yourServerKey].join('=')
  },
  json: {
    to: clientFirebaseToken,
    notification: {
      title: "Notification Title",
      body: "This is a neat little notification, right ?"
    }
  });

編輯

來自他們的 GitHub

自 2020 年 2 月 11 日起,請求已完全棄用。 預計不會出現新的變化。 事實上,已經有一段時間沒有人降落了。

如果你使用axios

axios({
  method: 'post',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    "Content-Type": "application/json",
    "Authorization": ['key', yourServerKey].join('=')
  },
  params: {
    to: clientFirebaseToken,
    notification: {
      title: "Notification Title",
      body: "Neat indeed !"
    }
  }
})

如果你使用的是 React JS/ React Native,使用 Axios 包可以很容易地完成,示例代碼如下,首先,你必須注冊 firebase 雲消息傳遞以獲取授權密鑰

axios.post(
          'https://fcm.googleapis.com/fcm/send',
          {
            data: {},
            notification: {
              title: "Sample text1",
              body: "Sample text2",
              image: "Sample text3",
            },
            to: '/topics/TopicName',
          },
          {
            headers: {
              Authorization:
                'key=Authorization key from firebase',
              
            },
          },
        )
        .then(Response => {
          console.log(Response.data);
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM