2016-08-30 45 views
0

我試圖從PHP發送通知給Android應用程序。我的PHP代碼在這裏。PHP + Android CURL Firebase通知錯誤

<?php 
require "int.php"; 
$message = $_POST['message']; 
$title = $_POST['title']; 
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; 
$server_key = "1:485869546397:android:bd503a78b6c26c35"; 
$sql = "select fcm_token from fcm_info"; 
$result = mysqli_query($con,$sql); 
$row = mysqli_fetch_row($result); 
$key = $row[0]; 
$headers = array(
    'Authorization:key=' .$server_key, 
    'Content-Type:application/json' 
); 

$fields = array('to'=>$key, 
    'notification'=>array('title'=>$title,'body'=>$message)); 

$payload = json_encode($fields); 

echo $payload; 

$curl_session = curl_init(); 
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm); 
curl_setopt($curl_session, CURLOPT_POST, true); 
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload); 
$result = curl_exec($curl_session); 
echo $result; 
?> 

和我張貼的數據mycode的我舉一個這樣的輸出: enter image description here

,我可以從火力控制檯發送通知。和服務器ID代碼是正確的。我不知道這是怎麼回事?你能給出一個建議或任何方式來解決這個問題?謝謝...

+0

50x錯誤通常意味着問題出現在服務器端。您可以在此處提出問題:https://firebase.google.com/support/ – pawelo

+0

您所獲得的下游消息響應正文不包含[documentation](https://firebase.google)中定義的一些字段.com/docs/cloud-messaging/http-server-ref#interpret-downstream),所以最好按照@pawelo的建議與Firebase支持部門聯繫 – GeorgeLBA

回答

1

這是經過測試的代碼,您可以使用下面的代碼發送通知。

public function sendGCM($message, $registration_ids) { 
    //FCM URL 
    $url = "https://fcm.googleapis.com/fcm/send"; 

    //prepare data 
    $fields = array (
     'registration_ids' => array ($registration_ids), 
     'data' => array ("message" => $message) 
    ); 
    $fields = json_encode ($fields); 

    //header data 
    $headers = array ('Authorization: key=<YOUR_API_KEY>', 'Content-Type: application/json'); 

    //initiate curl request 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_POST, true); 
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields); 

    // execute curl request 
    $result = curl_exec ($ch); 

    //close curl request 
    curl_close ($ch); 

    //return output 
    return $result; 
}