2016-04-06 4 views
0

Wenn ich versuche, eine gcm Nachricht über die unten php zu schicken, ich diesen Fehler:Google Cloud Messaging gibt ungültige Registrierung

{ "multicast_id": 8958574426215974401 "Erfolg": 0, "Fehler": 1 , "canonical_ids": 0, "results": [{"error": "InvalidRegistration"}]}

Der Server-API-Schlüssel ist jedoch genau das, was Google mir gegeben hat, und das gcm_token wird genau gespeichert meine Datenbank. Ich weiß nicht, wo das Problem liegt.

Hier ist meine Server-Seite 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; 
} 
?> 

Antwort

2

mysqli_query gibt Boolen Wert (wahr oder falsch), so wird dies geht nicht direkt funktioniert.

wir müssen mysqli_query Rückgabewerte in Array konvertieren mit fetch array, fetch assoc.

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

$registrationIds = array($results); 

-Code alle GCM Tokens combile.

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

den Sinn macht, wie ich Boolesche Werte bekommen, als ich var_dump(), um die mysqli_query. Vielen Dank! –

+0

Erwähnung nicht @ MichaelGulik –

Verwandte Themen