2016-04-06 143 views
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; 
} 
?> 

回答

2

mysqli_query返回Boolen值(true或false),所以這不會直接去工作。

我們必須使用fetch array,fetch assoc將mysqli_query返回值轉換爲數組。

$results = mysqli_query($Thesisdb, $sql); 

$registrationIds = array($results); 

代碼合併所有GCM令牌。

registrationIds=array(); 
$sql="SELECT `user_id`,`push_notification_registration_id` FROM `tbl1` WHERE `user_status`='Active' AND `push_notification_registration_id`!=''"; 
$fire=mysqli_query($this->conn,$sql); 
$users=array(); 
if((isset($fire))&&(!empty($fire))){ 
    while($result=mysqli_fetch_assoc($fire)){ 
     array_push($users,$result); 
    } 
} 
foreach($users as $single_user){ 
    $register_id=$single_user['push_notification_registration_id']; 
    array_push($registrationIds,$register_id); 
} 


$msg = array 
(
    'title'   => $news_title, 
    'subtitle'  => $news_description, 
    'tickerText' => 'Ticker Text', 
    'vibrate' => 1, 
    'sound'  => 1, 
    'news_id' => $news_id, 
    'notification_type' => 'news' 
); 

$fields = array 
(
    'registration_ids' => $registrationIds, 
    'data'    => $msg 
); 

$headers = array 
(
    'Authorization: key=' . YOUR_GOOGLE_API_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,true)); 
$result = curl_exec($ch); 
curl_close($ch); 
+1

這是有道理的,因爲我得到布爾值時,我var_dump()的mysqli_query。謝謝! –

+0

不提@MichaelGulik –