0
votes

i pass to the server a nickname and then i get registration id by nickname from my database.

GOOGLE_API_KEY is a server key generated by google gcm api.

this is the php code of the server:

<?php

require_once("db_config.php");


$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or      
die(mysqli_error());

$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());


$nicknames = $_POST['nicknames'];
$message = $_POST['message'];




$registration_ids = mysqli_query($con, "SELECT regId FROM Users WHERE nickname = '$nicknames'");
$numOfRows = mysqli_num_rows($registration_ids);

$row=mysqli_fetch_assoc($registration_ids );
printf ("%s\n",$row["regId"]);


if($numOfRows > 0) {

$url = 'https://gcm-http.googleapis.com/gcm/send';
//$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => array($registration_ids), 'data' => array("message" => $message),);
$headers = array('Authorization: key=' . GOOGLE_API_KEY, 'Content-Type: application/json');

    $ch = curl_init();

    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));

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

    curl_close($ch);
    echo $result;
}
else {
    echo 'false';
}     

?>

after i send the message, i recieve:

{"multicast_id":4772095405461687926,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}]

i also printed to my self the registartion id that i get from my database and it is matched to the device`s registration id.

ty for your help.

2

2 Answers

1
votes

ok i resolved my problem. the problem was that in this line "array($registration_ids)", the $registration_ids is a result object, so i changed it to $row["regId"] and than i got the registration id.

now it works.

0
votes

You should check the registration token that you are passing to the server. See Downstream message error response codes.

You may also look at this documentation which suggests to check the validity of the registration token.