从heroku php服务器通过firebase发送android通知

时间:2016-12-16 21:23:12

标签: php android heroku firebase

我正在尝试使用heroku PHP服务器将通知推送到特定的Android设备。但是,我没有运气。

我可以通过firebase控制台推送通知就好了(即问题不在我的Android应用程序中)。

这是我的代码(我从How do I send a POST request with PHP?获得):

$url = 'https://fcm.googleapis.com/fcm/send';
$data = array('score' => '5x1', 'time' => '15:10');

// use key 'http' even if you send the request to https://...
$options = array(
  'http' => array(
    'header'  =>  "Content-type: application/json\r\n" .
                  "Authorization: key=MY_SERVER_KEY\r\n",
    'method'  => 'POST',
    'data' => http_build_query($data),
    'to' => 'MY_FCM'
  )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

我觉得我正在做一些非常基本的错误(比如我的JSON格式不正确或者其他东西)。

可以在此处找到firebase apis:https://firebase.google.com/docs/cloud-messaging/send-message

我已经在这方面工作了几天,我们将非常感谢任何帮助。谢谢你们!

更新

快速说明Heroku不支持我所经历的HttpRequest()类,但是,cURL工作得很好。此外,我没有提及它,但我实际上想要发送通知消息,而不仅仅是数据消息。所以,我的最终代码如下所示:

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n
    \"notification\" : {\n
    \"body\" : \"Goku\",\n
    \"title\" : \"Over 9000\",\n
    },\n
    \"to\" : \"MY_FCM_TOKEN\"\n
    \"priority\" :
    \"high\"\n
    }",
  CURLOPT_HTTPHEADER => array(
    "authorization: key=MY_SERVER_KEY",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

1 个答案:

答案 0 :(得分:1)

在您的代码中

'http' => array(
    'header'  =>  "Content-type: application/json\r\n" .
              "Authorization: key=MY_SERVER_KEY\r\n",
    'method'  => 'POST',
    'data' => http_build_query($data),
    'to' => 'MY_FCM'
)

您必须发送数据并进入密钥“内容”。

/* $mydata contains 'data' and 'to' */
'http' => array(
    'header'  =>  "Content-type: application/json\r\n" .
              "Authorization: key=MY_SERVER_KEY\r\n",
    'method'  => 'POST',
    'content' => http_build_query($mydata)
 )

这些是使用php发送fcm通知的几种推荐方法

的HttpRequest

$request = new HttpRequest();
$request->setUrl('https://fcm.googleapis.com/fcm/send');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/json',
  'authorization' => 'key=YOUR_FCM_API_KEY'
));

$request->setBody('{
  "data" : {
  "name" : "Goku",
  "power_level" : "Over 9000",
  "fighting_skill" : "excellent"
 },
  "to" : "FCM_ID_OF_RECIEVER"
 }');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

pecl_http

<?php

$client = new http\Client;
$request = new http\Client\Request;

$body = new http\Message\Body;
$body->append('{
  "data" : {
    "name" : "Goku",
    "power_level" : "Over 9000",
    "fighting_skill" : "excellent"
  },
  "to" : "FCM_ID_OF_RECIEVER"
}');

$request->setRequestUrl('https://fcm.googleapis.com/fcm/send');
$request->setRequestMethod('POST');
$request->setBody($body);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/json',
  'authorization' => 'key=YOUR_FCM_API_KEY'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

卷曲

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"data\" : {\n    \"name\" : \"Goku\",\n    \"power_level\" : \"Over 9000\",\n    \"fighting_skill\" : \"excellent\"\n  },\n  \"to\" : \"FCM_ID_OF_RECIEVER\"\n}",
  CURLOPT_HTTPHEADER => array(
    "authorization: key=YOUR_FCM_API_KEY",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}