2

This is the code I'm using to register my app:

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (!GCMRegistrar.getRegistrationId(getApplicationContext()).equals("")) {
    GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}

Here are my manifest permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.cidaut.blueparking.permission.MAPS_RECIEVE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

Here's the Reciever

<service android:name=".GCMIntentService" />

<receiver
    android:name="com.google.android.gcm.GCMBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.cidaut.blueparking" />
    </intent-filter>
</receiver>

And here's my com.cidaut.blueparking.GCMIntentService.java class

package com.cidaut.blueparking;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {

    }

    @Override
    protected void onError(final Context arg0, final String arg1) {
            System.out.println("ERROR'D!!");
    System.out.println(arg1);
    }

    @Override
    protected void onMessage(final Context arg0, final Intent arg1) {
        System.out.println("YES HELLO I AM DEBUGGER");
        String message = "Tu aparcamiento ha exiprado.";
        if (arg1.getExtras() != null) {
            final Bundle extras = arg1.getExtras();
            if (extras.containsKey("message")) {
                message = extras.getString("message");
            }
        }

        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setContentTitle("Blue Parking").setContentText(message)
                .setSmallIcon(R.drawable.ic_launcher);

        final Intent resultIntent = new Intent(this, MapScreen_.class);
        final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(resultIntent);

        final PendingIntent resultPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        try {
            final Uri notification = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            final Ringtone r = RingtoneManager.getRingtone(
                    getApplicationContext(), notification);
            r.play();
        } catch (final Exception e) {
        }
        notificationManager.notify(1, mBuilder.build());
    }

    @Override
    protected void onRegistered(final Context arg0, final String arg1) {
        System.out.println("TOKEN: " + arg1);
    }

    @Override
    protected void onUnregistered(final Context arg0, final String arg1) {
        System.out.println("UNREGISTERED");
        System.out.println(arg1);
    }

}

So, when I call GCMRegistrar.register it does never call onRegistered... unless I try it on my Nexus 4. The code is working flawlessly on my Nexus 4. I've tested it on Samsung Galaxy S, Samsung Galaxy Ace, Samsung Galaxy SII, Samsung Galaxy SIII, HTC Wildfire S, HTC Desire, HTC One X and ASUS Transformer, and it simply won't work. If it didn't work on my Nexus 4 I'd be a lot less confused. What am I doing wrong here?

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • 1
    Add to the constructor a call to `super(SENDER_ID)`, if it works then add the function `getSenderIds(Context context)` – AlexBcn May 06 '13 at 15:04
  • Added the call. Nothing changed. – Charlie-Blake May 06 '13 at 15:45
  • I was going to say to add the package before the service .GCMIntebtService but it will be worthless according to (http://stackoverflow.com/questions/11332034/google-gcmintentservice-instantiate) [http://stackoverflow.com/questions/11332034/google-gcmintentservice-instantiate] << Look here – AlexBcn May 06 '13 at 15:58

1 Answers1

1

You are not making call to register. Remove the '!' or use this code.

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {     
GCMRegistrar.register(this, SENDER_ID);
}

It may be working on your Nexus 4 because registration id for that is already present. Uninstall the applicationa after clearing the data on nexus 4 and try to run it again.

Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47