无法使用Firebase REST API发送批处理消息 [英] Failed to send batch messages using Firebase REST API

查看:213
本文介绍了无法使用Firebase REST API发送批处理消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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内容,类似于

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

Content-Type",多部分/混合";boundary =-subrequest_boundary

Content-Type", "multipart/mixed; boundary=--subrequest_boundary

Content-Type",多部分/混合";boundary = subrequest_boundary

Content-Type", "multipart/mixed; boundary=subrequest_boundary

脚本

#!/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_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
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--

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆