【问题标题】:how to i send HTTP request for sending notification in firebase?如何发送 HTTP 请求以在 firebase 中发送通知?
【发布时间】:2020-01-05 08:36:08
【问题描述】:

要发送通知,您需要发送以下 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 firebase-cloud-messaging


    【解决方案1】:

    如果您使用的是 Node.JS,我建议您查看 Firebase 的 Node.JS SDK 的文档,而不是手动发送 HTTP 请求。有the official documentationthis nice tutorial

    如果您仍想使用纯 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 ?"
        }
      });
    

    编辑

    来自their 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 !"
        }
      }
    })
    

    【讨论】:

    • 我在使用 Node.JS Firebase SDK 向 IOS 设备发送静默推送通知时遇到了很大的麻烦 - 只有十分之一的设备实际接收到它们 - 我现在选择直接 HTTP,而不是如上所述(手指交叉)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多