gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:50:07 25 4
gpt4 key购买 nike

我正在尝试使用 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 api: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;
}

最佳答案

在你的代码中

'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'
)

您必须将数据发送到 key “content”中。

/* $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 通知的几种推荐方法

HTTP请求

$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();

curl

<?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;
}

关于php - 从 heroku php 服务器通过 firebase 发送 android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com