我想從服務器使用firebase雲消息傳遞系統向我的android設備發送推送通知。我能夠成功註冊我的設備併爲我的設備生成令牌。我無法使用下面的腳本Android推送通過firebase通知(服務器端)
<?php
function send_notification($token1,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array('registration_ids'=>$token1,'data'=>$message);
$header = array('Authorization:key = <my key here>','Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result===false)
{
die('Curl failed'.curl_error($ch));
}
curl_close($ch);
return $result;
}
require "init.php"; //my db details here
$sql = "select token from fbuser";
$result = mysqli_query($con,$sql);
$tokens = array();
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
$tokens[] = $row["token"]; // i was able to successfully echo out token as well
}
}
mysqli_close($con);
$message = array("message"=> "This is a test message");
$message_status = send_notification($tokens,$message);
echo $message_status; ***//Trouble here. it just prints out "To" whenever i hit the php script***
?>
將通知發送到我的設備現在,只要我打的劇本,只是響應返回
To
,並沒有別的..並沒有通知交付以及。無法找出問題。請求你的指導。
感謝
Thanks.much。但沒有運氣將registration_ids中的值更改爲「To」。我嘗試使用你的方法和郵遞員給我字段「數據」必須是一個JSON數組: – Sriram
這意味着有問題在cretering json – Killer
道歉,你能幫我在這裏請 – Sriram