0
當我試圖通過以下PHP發送GCM消息,我得到這個錯誤:谷歌雲消息返回無效的註冊信息
{「multicast_id」:8958574426215974401,「成功」:0,「失敗」:1 ,「canonical_ids」:0,「results」:[{「error」:「InvalidRegistration」}]}
但是,服務器API密鑰正是Google給我使用的,並且gcm_token被準確存儲到我的數據庫。我不知道問題出在哪裏。
這裏是我的服務器端的PHP:
<?php
require 'db_connect.php';
// API access key from Google API's Console
define('API_ACCESS_KEY', 'AIzaSyREST OF MY KEY');
//insert the message into a database
$sql = "SELECT gcm_token FROM users WHERE user_id='1' LIMIT 1;";
// preform
if (!mysqli_query($Thesisdb, $sql)) {
printf("Errormessage: ", mysqli_error($Thesisdb));
mysqli_close($Thesisdb);
} else{
$results = mysqli_query($Thesisdb, $sql);
$registrationIds = array($results);
// prep the bundle
$msg = array
(
'message' => '$user',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_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);
echo $result;
}
?>
這是有道理的,因爲我得到布爾值時,我var_dump()的mysqli_query。謝謝! –
不提@MichaelGulik –