问题描述
我正在尝试使用REST API在Firebase Cloud Messaging中发送批处理消息.我在C#中准备了一个多部分HTTP请求:
I am experimenting with the REST API for sending batch messages in Firebase Cloud Messaging. I prepared a multipart HTTP request in C#:
using var request = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/batch");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "xxx");
request.Content = new StringContent(multicast);
request.Content.Headers.Remove("Content-Type");
request.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=--subrequest_boundary");
var response = await FcmHttpClient.SendAsync(request);
上方 multicast
字段的字符串值是HTTP内容,类似于 Firebase文档:
The string value of the multicast
field above is an HTTP content similar the one provided in the Firebase documentation:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer xxx
POST /v1/projects/myproject/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"global",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer xxx
POST /v1/projects/myproject/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"readers-club",
"notification":{
"title":"Price drop",
"body":"2% off all books"
}
}
}
--subrequest_boundary--
Firebase服务器返回Bad Request-400,并显示错误消息:"未能解析批处理请求,错误:0个项目.收到的批处理主体:--subrequest_boundary-"
,表示Firebase使用终止的-subrequest_boundary-
直接处理内容.
Firebase server returns Bad Request-400 with error message: "Failed to parse batch request, error: 0 items. Received batch body: --subrequest_boundary--"
which indicates that Firebase directly handles the content with the terminating --subrequest_boundary--
.
什么可能是问题的原因?
What could be the cause of the problem?
推荐答案
昨天,我需要编写bash脚本来发送Bath FCM通知,并查看您的代码@Ugur,谢谢.现在工作了,你需要改变
yesterday, i need write bash script to send bath FCM notifaction, and see your code @Ugur, thanks.now its working, u need change
到
脚本
#!/bin/bash
curl \
-X POST \
-H "Authorization: Bearer [token_auth]" \
-H "Content-Type: multipart/mixed; boundary=subrequest_boundary" \
--data-binary @test2.txt \
https://fcm.googleapis.com/batch
和test2.txt,示例发送2条通知
and test2.txt, example send 2 notification
-subrequest_boundary内容类型:application/http内容传输编码:二进制授权:承载[token_auth]
--subrequest_boundaryContent-Type: application/httpContent-Transfer-Encoding: binaryAuthorization: Bearer [token_auth]
POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"[token_device]",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!",
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]
POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"[token_device]",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!",
}
}
}
--subrequest_boundary--
- 将[project_name_firebase]更改为您在firebase中的项目的名称控制台示例:project_3323.
- 使用令牌目标设备更改[token_device].
- 使用您的Google身份验证凭据令牌更改[token_auth].
- java: https://developers.google.com/identity/protocols/oauth2/service-account#java_1
- phyton: https://developers.google.com/identity/protocols/oauth2/service-account#python_2
- 卷曲: https://developers.google.com/identity/protocols/oauth2/service-account#httprest
通知配置: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidConfig
获取[token_auth]
to get [token_auth]
获取[token_auth],getToken.sh的代码
code for get [token_auth], getToken.sh
#!/bin/bash
client_email="[email_project_firebase]"
service="https://oauth2.googleapis.com/token"
time=3600
scope="https://www.googleapis.com/auth/cloud-platform"
private_key=$(echo -ne "[private_key]")
datenow="$(date +%s)"
expired="$(( datenow + time ))";
header='{"alg": "RS256","typ": "JWT"}'
payload='{"iss": "'$client_email'","scope": "'$scope'","aud": "'$service'","exp": '$expired',"iat": '$datenow'}'
HEADEREnc=$( echo -n "${header}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
PAYLOADEnc=$( echo -n "${payload}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
data="${HEADEREnc}"."${PAYLOADEnc}"
signature=$( openssl dgst -sha256 -sign <(echo -n "${private_key}") <(echo -n "${data}") | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
JWT="${data}"."${signature}"
jsonDataToken=$(curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='$JWT https://oauth2.googleapis.com/token)
tokenAuth=$(echo -n $jsonDataToken |grep -Po '"access_token":.*?[^\\]"'|cut -d ':' -f2 |sed 's/"//g')
echo $tokenAuth
[email_project_firebase],[private_key]您可以从"firebase控制台"中的文件json中获取它.->项目名称->项目设置或点击齿轮图标->帐户服务->单击按钮创建新密钥"
[email_project_firebase],[private_key] u can get it from file json in "firebase console" -> project name -> project setting or click gear icon -> account services -> click button "create new secret key"
列出其他服务的范围 https://developers.google.com/identity/protocols/oauth2/scopes
这篇关于无法使用Firebase REST API发送批处理消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!