2014-09-22 29 views
0
$registrationIds=array("My Key"); 
$fields = array 
(
    'registration_ids' => $registrationIds, 
    'data'   => array 
(
    'message'=>'here is a message. message', 

) 
); 
$key="1234444"; 
$headers = array 
(
    'Authorization:key='.$key, 
    'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
return $result; 

輸出溶液:機器人推送通知與這些消息失敗任何一個有此

{multicast_id:7750113442176841000,成功:0,故障:1,canonical_ids:0,結果:[{錯誤:「 MismatchSenderId」}]}

回答

0

我的工作代碼:

function send_push_notification($registatoin_ids, $message) { 


    // Set POST variables 
    $url = 'https://android.googleapis.com/gcm/send'; 

    $fields = array(
     'registration_ids' => $registatoin_ids, 
     'data' => $message, 
    ); 

    $headers = array(
     'Authorization: key=' . GOOGLE_API_KEY, 
     'Content-Type: application/json' 
    ); 
    //print_r($headers); 
    // Open connection 
    $ch = curl_init(); 

    // Set the url, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Disabling SSL Certificate support temporarly 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    // Execute post 
    $result = curl_exec($ch); 
    if ($result === FALSE) { 
     die('Curl failed: ' . curl_error($ch)); 
    } 

    // Close connection 
    curl_close($ch); 
    echo $result; 
} 
+0

有什麼不同? – sje397 2014-09-22 05:49:21

+0

你的代碼是正確的。你使用正確的項目ID嗎? – Andy 2014-09-22 05:55:30

+0

https://code.google.com/apis/console/#project:xxxxxxxxxxx請檢查 – Andy 2014-09-22 05:56:21

0

鑑於你所描述的錯誤,您正在使用不正確的SENDER_ID關鍵。

的使用權SENDER_ID是在你的開發人員網站的項目編號:

https://console.developers.google.com/project/XXXXXXX/apiui/credential?authuser=0

是XXXXXX是你必須用來發送推notificationes發件人ID鍵。

更多信息here