node.js - 如何使用 google apis 向多个设备发送 FCM 推送通知?
问题描述
我想一次向多个设备发送推送通知。我可以传递令牌数组吗?可以使用google api吗?请帮我解决一下这个。
request(
{
method: 'POST',
uri:
'https://fcm.googleapis.com/v1/projects/projectId/messages:send',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${token}`,
'apns-priority': 'high',
content_available: true
},
body: {
message: {
token: token,
notification: {
body: 'This is an FCM notification message!',
title: 'FCM Title'
},
android: {
notification: {
sound: 'default'
}
}
}
},
json: true
},
async function(error, response, body) {
if (error) {
console.log(error);
}
console.log(body);
}
);
解决方案
If you want to make the POST requests by yourself, you can prefer to:
1) Use the legacy HTTP server protocol. With that you will be able to use the "registration_ids" field which expects an array of token strings in POST body. Docs here.
2) Stick to HTTP v1 API that you're using right now, but since "token" field is expecting only string, you can first subscribe your users to a specific topic and use that topic in "topic" field in POST body.
Additionally, you may prefer to use Admin SDK to send batch notifications. You can check for further here.
推荐阅读
- c# - WebClient UploadString 返回html代码,不发送数据
- sql-server - 有没有办法将 sql 视图导出为可执行文件
- mysql - 奇怪的行为 - phpMyAdmin 和 VS 2017 中相同 SQL 的不同结果
- amazon-web-services - 更换ec2实例后,负载均衡器需要很长时间才能建立连接
- java - Tasklet#execute 什么时候应该返回 CONTINUABLE?
- typescript - 如何在许多 Web 组件中共享 1 个 lit-element 实例
- javascript - 如何修复由于 React SSR 初始化数据库和云功能 firebase 初始化数据库而多次初始化的 firebase 数据库?
- php - PHP Copy 有效,但重命名无效
- c# - SignedData 给出了无效的算法指定。异常
- javascript - 更新嵌套数组并在此之前使用 mongoose 填充