Viewed   116 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, November 30, 2022

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/>";

Sunday, November 13, 2022

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 ''
Saturday, September 24, 2022

2 things:

  1. You should use process.env.PORT for handling the named pipe port in Azure App Service. See Listen additional port Microsoft Azure Nodejs.

  2. If #1 didn't solve your problem, you'll need to enable logging of stdout and stderr for further troubleshooting. See How to enable BLOB-logging for a Node.js Api App on Azure?

Tuesday, October 25, 2022

I actually had the same problem, and was researching more than 10 hours.

I finally found out the problem! Nothing related to the Server API key or Browser API Key or SenderID. The problem was the Google documentation:

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");

Google says that you have to call the getRegistrationId function and only if the id is empty call register! Which did not work for me at all... when I did that I always got back MismatchSenderId when sending to this regId.

My solution was: Always call

GCMRegistrar.register(this, SENDER_ID);

and when the function

protected void onRegistered( Context c, String regId )

is called save the regId in my server database.

if I do it this way, all works fine!

Wednesday, November 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :