简体   繁体   English

如何发送 HTTP 请求以在 firebase 中发送通知?

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

To send a notification, you'll need to send the following HTTP request:要发送通知,您需要发送以下 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" }

how can I do this??我怎样才能做到这一点??

If you're using Node.JS, I suggest you look at the documentation for Firebase's Node.JS SDK instead of manually sending HTTP requests.如果您使用的是 Node.JS,我建议您查看 Firebase 的 Node.JS SDK 文档,而不是手动发送 HTTP 请求。 There's the official documentation or this nice tutorial官方文档这个不错的教程

If you still want to go for the plain HTTP method, you can use the request npm module如果你仍然想使用普通的 HTTP 方法,你可以使用request npm 模块

$ npm install request

then in your code :然后在你的代码中:

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 ?"
    }
  });

Edit编辑

From their GitHub来自他们的 GitHub

As of Feb 11th 2020, request is fully deprecated.自 2020 年 2 月 11 日起,请求已完全弃用。 No new changes are expected to land.预计不会出现新的变化。 In fact, none have landed for some time.事实上,已经有一段时间没有人降落了。

If you use axios如果你使用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 !"
    }
  }
})

If you are using React JS/ React Native, using the Axios package it can be done very easily, the sample code is below, first, you have to register for firebase cloud messaging to get the authorization key如果你使用的是 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.

相关问题 如何发送 HTTP GET 请求到 POSTMAN 上的 Firebase Realtime DB? - How to send HTTP GET request to Firebase Realtime DB on POSTMAN? 如何使用 firebase 在 android 中的通知中发送图像? - how to send image in notification in android using firebase? 如何在不使用 Firebase 控制台的情况下发送 Firebase 云消息通知? - How can I send a Firebase Cloud Messaging notification without use the Firebase Console? 如何在节点和快递中发送 firebase 主题通知? - how to send firebase topic notification in node and express? 我如何只知道 Button OnClickListener 中的用户 ID 就可以向特定用户发送推送通知? Firebase - How can i send push notification to specific users just knowing the userID inside Button OnClickListener? Firebase 从服务器发送通知时,一次可以发送多少个令牌? - How many tokens can I send at a time when sending a notification from the server? 我试图通过 firebase 控制台 api 发送通知 - i was trying to sending notification through firebase console api 在使用 reactjs 发送 POST 请求之前,我如何等待 firebase 检查用户是否有效? - how can i wait for firebase to check the user is valid before sending a POST request with reactjs? 如何通过 HTTP 向观众发送 firebase 通知 - How to send firebase notifications to audience via HTTP 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM