Hussainsoni
Hussainsoni

Reputation: 87

Always get the invalid registration from the php (server side) in GCM

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

I am always return this thing with my GCM programming. I have correctly enter the Project id on the android manifest. I have give correct google API for the connect the API. here is my php code. I have store the api_key in the other file and that i include in this. Can anyone tell why i getting this error.

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

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

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

    $cn = curl_init();


    curl_setopt($cn, CURLOPT_URL, $url);

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


    curl_setopt($cn, CURLOPT_SSL_VERIFYPEER, false);

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


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


    curl_close($cn);
    echo $result;

Upvotes: 2

Views: 10241

Answers (4)

EffyCoder
EffyCoder

Reputation: 48

  • First of all, you should check the registration token taken with trailing spaces removed is valid using Firebase console. If there isn't a problem with sending message to a specific device.

  • Then you should check the size of the field used to store the registration token in your database.In my case what happened I use phpMyAdmin, which uses MySQL/MariaDb database.And I have given length of 80 for varchar which caused truncating the registration token to the length of 80. So, it became invalid one. That is why I was getting 'InvalidRegistration' error. So I changed length to 200 which I think is enough. Wish this helps you.

Upvotes: 0

s.schleu
s.schleu

Reputation: 1361

InvalidRegistration-error is returned when a Registration ID (which you pass in via $registatoinids) is invalid, this error basically has nothing to do with your PHP-code or API-Key.

Just check out the registration id of the device you are trying to send the notification to and pass the (new) id when calling

'registration_ids' => $registatoinids

If your device does not have any reg-id, just re-register. Please refer to this section of the documentation.

Upvotes: 0

Rubanraj Ravichandran
Rubanraj Ravichandran

Reputation: 1293

Did u defined the vaule for API_KEY in your file.? See below php file. This is working for me.

<?php
    //Generic php function to send GCM push notification
   function sendMessageThroughGCM($registatoin_ids, $message) {
        //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
        // Update your Google Cloud Messaging API Key
        define("GOOGLE_API_KEY", "API_SERVER_KEY");         
        $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);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);   
        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;
    }
?>

Define your API_SERVER_KEY in this line.

define("GOOGLE_API_KEY", "API_SERVER_KEY");

refer this site : http://programmerguru.com/android-tutorial/how-to-send-push-notifications-using-gcm-service/

Upvotes: 1

Seshu Vinay
Seshu Vinay

Reputation: 13588

"InvalidRegistration" means Registration id that you sent is not registered. Check API key and registration id correctly.

Upvotes: 0

Related Questions