node.js - 如何发送 HTTP 请求以在 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 ?"
}
});
编辑
自 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 !"
}
}
})
推荐阅读
- r - 替换R中数据框中的多个值
- vba - Excel vba 检查 Match 函数是否返回 NA - 宏崩溃:运行时错误 1004“应用程序定义或对象定义错误”
- javascript - Next.JS:无法在 getInitialProps 中调用内部 API。出现错误:读取 ECONNRESET
- angular - 自定义验证 - if 语句
- tvos - 是否可以通过无线方式将企业/内部 tvOS 应用程序分发到 Apple TV?
- .net - 如何在 Azure AD B2C 登录的 webview 屏幕中删除 URL
- tabs - 如何将 konsole 选项卡放在左侧?
- ssl - 在非阻塞模式下,openssl 允许在客户端使用任何证书
- reactjs - Jest,Enzyme,React - 测试 Iframe OnLoad
- javascript - 从 Dropzone.js 获取全尺寸图像(不是缩略图大小)的数据 URI