Asked  7 Months ago    Answers:  5   Viewed   34 times

I'm trying to send a notification using GCM on my android device, but I always get InvalidRegistration error.

Here is the PHP code that is supposed to send the notification:


$registrationIds = array( $_GET['id']);

// prep the bundle
$msg = array
    'message'   => 'TestMessage',
    'title'     => 'TestTitle',
    'subtitle'  => 'TestSubtitle',
    'tickerText'    => 'TestTicker',
    '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, '' );
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;

This is how the registration is done:

private void registerInBackground() {
new AsyncTask<Void, Object, String>() {
    protected String doInBackground(Void... params) {
        String msg = "";
        try {
            if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(context);
            regid = gcm.register(SENDER_ID);
            msg = "Device registered, registration ID=" + regid;

            //Here I save the registration ID, and when I try to use it manually, by running the PHP script above, I get the error.

            // Persist the registration ID - no need to register again.
            storeRegistrationId(context, regid);
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
        return msg;

    protected void onPostExecute(String msg) {
        Toast.makeText(context, msg + "n", Toast.LENGTH_SHORT).show();

}.execute(null, null, null);

After running the application, and calling the method that registers the app, I get a registration ID. Then, when I type localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx I get




Invalid Registration ID Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the intent and that you're not truncating it or adding additional characters. Happens when error code is InvalidRegistration.

So basically you are making an error in sending the DeviceID received by the phone to the server. Make sure your code does not alter the deviceId in any way. You have not posted the code for accepting the Id's. There is a mistake in that part or the storing of the DeviceId's. I don's think there is any error in the code you have posted as the errorLog show's Invalid Registration

Wednesday, March 31, 2021
answered 7 Months ago

Oh, I completely forgot about this question, sorry for that. I already find out what was the problem. Earlier I used that code which I posted before to send messages via C2DM. I made only a few improvements to adjust it to GCM. And one of variables ($msgExtra) could equal null in some case. It was intended behaviour and with C2DM it worked just fine. Unfortunately, when you try to pass null in JSON via GCM you get 404 error, although GCM documentation says nothing about that... So code which I posted is good as far as you don't try to send null. Solution of my problem is to replace null value with something like "". And once again - sorry that I post it just now, I completely forgot about this question.

Saturday, May 29, 2021
answered 5 Months ago

Try using this php script , make sure that the .pem certificate exits in same path as that php script when you run it , also get a correct device token

        /* We are using the sandbox version of the APNS for development. For production
        environments, change this to ssl:// */
        $apnsServer = 'ssl://';
        /* Make sure this is set to the password that you set for your private key
        when you exported it to the .pem file using openssl on your OS X */
        $privateKeyPassword = '1234';
        /* Put your own message here if you want to */
        $message = 'Welcome to iOS 7 Push Notifications';
        /* Pur your device token here */
        $deviceToken =
        /* Replace this with the name of the file that you have placed by your PHP
        script file, containing your private key and certificate that you generated
        earlier */
        $pushCertAndKeyPemFile = 'PushCertificateAndKey.pem';
        $stream = stream_context_create();

        $connectionTimeout = 20;
        $connection = stream_socket_client($apnsServer,
        if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        } else {
        echo "Successfully connected to the APNS. Processing...</br>";
        $messageBody['aps'] = array('alert' => $message,
        'sound' => 'default',
        'badge' => 2,
        $payload = json_encode($messageBody);
        $notification = chr(0) .
        pack('n', 32) .
        pack('H*', $deviceToken) .
        pack('n', strlen($payload)) .
        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
        if (!$wroteSuccessfully){
        echo "Could not send the message<br/>";
        else {
        echo "Successfully sent the message<br/>";

Tuesday, August 3, 2021
answered 3 Months ago

So the issue is that you have dependencies that inlcude old versions of the support library. Check this thread out:

Android UrbanAirship Crash in takeOff

Doing this might help (from the thread):

configurations.all {
    resolutionStrategy {
        force ''
        force ''
        force ''
Wednesday, August 11, 2021
answered 3 Months ago

try below code to open any link on page to new tab & switch to that tab. Perform operations there & go back to first tab for further execution.

WebDriver driver = new FirefoxDriver();
        WebElement e = driver.findElement(By.xpath(".//*[@id='nav-questions']"));       
        Actions action = new Actions(driver); 
        action.keyDown(Keys.CONTROL).build().perform(); //press control key;
        Thread.sleep(10000); // wait till your page loads in new tab
        action.keyUp(Keys.CONTROL).build().perform(); //release control key
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); //move to new tab
        driver.navigate().refresh(); // refresh page
        driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); //switch to first tab
        driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo
Wednesday, September 22, 2021
Joseph Szymborski
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :