2016-04-08 201 views
0

我嘗試發送消息,但什麼也沒收到。雖然沒有失敗,當打印變量時:無法發送推送通知

$result 

這個返回「to」(我不知道爲什麼)。

我使用的代碼是:

private function sendMessageGcm($registration_id,$message){    
    $this->key = "xxxxxxxxxxxxxxxxxxxxxx"; 
    $data = array(
    "registration_id" => $registration_id, 
    "data" => $message 
); 
    $headers = array(
    "Content-Type:application/json", 
    "Authorization:key=" . $this->key 
); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
    $result = curl_exec($ch); 
    if($result == false) { 
    echo('Curl failed: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    $rtn["code"] = "000";//means result OK 
    $rtn["msg"] = "OK"; 
    $rtn["result"] = $result; 
    return($rtn); 
} 
+0

你是怎麼稱呼這個功能的?沒有我看到調用代碼的地方?還要在'<?php'後面加上'error_reporting(E_ALL); ini_set('display_errors',1);'來檢查錯誤。 –

回答

5

$data$registration_id必須是一個數組與推送通知工作。所以它應該是這樣的。

$data = array(
    "registration_ids" => array($registration_id), 
    "data" => array(
     "body" => $message, 
    ), 
);