简体   繁体   中英

FCM HTTP v1 Batch request won't work in PHP

I'm using currently the Firebase messaging with PHP. I was able to make it work with a single notification with PHP and cURL. I've read the documentation about making batch request and I've constructed the request string as follow:

--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.xxxxxnY


POST /v1/projects/xxxxxxxx/messages:send
Content-Type: application/json
accept: application/json

{
 "message":{
  "token":"cxxxxx3",
  "data":{
   "typeNoti":"paiement",
   "idcompte":"admin",
   "typecompte":"paiement"
  },
  "notification":{
   "title":"Test1",
   "body":"Notification de test 1"
  }
 }
}

--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer yaxxxxxxY

POST /v1/projects/xxxxxx/messages:send
Content-Type: application/json
accept: application/json

{
 "message":{
  "token":"eoxxxxxU1",
  "data":{
   "typeNoti":"paiement",
   "idcompte":"compte2",
   "typecompte":"paiement"
  },
  "notification":{
   "title":"Test1",
   "body":"Notification de test 1"
  }
 }
} 
--subrequest_boundary--

This string is generated in a file called batch_request.txt as the documentation said. When trying to send the request with cURL I receive this error:

{
  "error": {
    "code": 400,
    "message": "Failed to parse batch request, error:  0 items. Received batch body: (0) bytes redacted",
    "status": "INVALID_ARGUMENT"
  }
}

I've experienced a strange behavior also, which is when I construct that string manually (in PHP I use HEREDOC), the batch request does occurs with 200ok response. I've tried to find some hidden character but couldn't.

I know there is a library called firebase-php . The problem with it is that I cannot find a straight foreward example like this:

//authenticate with json file
//create new token if it doesn't exist or skip
//create notification array
//create data array
//send multi notifications to multiple devices

I actually use a for loop and mono notification (one device token, one message at a time). The problem with this is that not all notifications are sent.

I appreciate your help thanks!

I gathered the code from firebase-php docs. After installing the library with composer I get a lot of errors and a lot of missing files in Firebase directory inside vendor like Factory.php etc

My solution was to NOT USE firebase-php library due to the numerous bugs I've faced (or maybe misuses) neither to construct the REST request text file for batch request. I used instead the NodeJS SDK which worked like a charm. I'm running a javascript file from my php code using exec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM