1

I am developing an application where i have to register my device with GCM but it is not registering my device. i have granted all permissions to the applications in the menifest my log is here

Method to register onGCM is

    public void register()
        {
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);

            if (GCMRegistrar.isRegistered(this))
                {
                    Log.d("info", GCMRegistrar.getRegistrationId(this));
                }

            final String regId = GCMRegistrar.getRegistrationId(this);
            System.out.println("registration ID is " + regId);
            if (regId.equals(""))
                {
                    System.out.println("registration ID is " + regId);
                    GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
                    Log.d("info", GCMRegistrar.getRegistrationId(this));
                    System.out.println("Get Reg ID: " + GCMRegistrar.getRegistrationId(this));
                }
            else
                {
                    Log.d("info", "already registered as" + regId);
                }
        }

and my GCM service is

public class GCMService extends GCMBaseIntentService
{
    public GCMService()
        {

            super(CommonUtilities.SENDER_ID);
        }

    public void generateNotification(Context context, String message)
        {
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher, message, when);
            String title = context.getString(R.string.app_name);

            Intent notificationIntent = new Intent(context, OffersActivity.class);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
            notificationManager.notify(0, notification);
        }

    @Override
    protected void onError(Context arg0, String arg1)
        {
        }

    @Override
    protected void onMessage(Context arg0, Intent arg1)
        {
            Log.d("GCM", "RECIEVED A MESSAGE");             
            generateNotification(arg0, "New Offers Are Available");
        }

    @Override
    protected void onRegistered(Context arg0, String arg1)
        {
        }

    @Override
    protected void onUnregistered(Context arg0, String arg1)
        {
        }
}

and my menifest is

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<!-- Map Location permissions -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Notification permissions -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />    

<permission
    android:name="myPackage.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<!-- Storage permissions -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Internet permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Call Phone permissions -->
<uses-permission android:name="android.permission.CALL_PHONE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="cmyPackage.SplashScreen"
        android:label="AppName"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="myPackage.CatagoriesActivity"
        android:configChanges="keyboardHidden|orientation|keyboard"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <activity
        android:name="myPackage.OffersActivity"
        android:configChanges="keyboardHidden|orientation|keyboard"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <activity
        android:name="myPackage.SettingsActivity"
        android:configChanges="keyboardHidden|orientation|keyboard"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <activity
        android:name="myPackage.OfferDetailsAtivity"
        android:configChanges="keyboardHidden|orientation|keyboard"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <activity
        android:name="myPackage.DealInfoActivity"
        android:configChanges="keyboardHidden|orientation|keyboard"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <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="myPackage" />
        </intent-filter>
    </receiver>
    <service android:name=".GCMService" />        

    <uses-library android:name="com.google.android.maps" />
</application>    


I/System.out(17369): Registering
D/GCMRegistrar(17369): resetting backoff for packageName
V/GCMRegistrar(17369): Registering app packageName of sendersID xxxxxxxxxxxxxx
registration id     Still Empty
(17369): Not yet registered

i have multiple packages in my application

public final class CommonUtilities
{
    public static final String SENDER_ID = "xxxxxxxxxxxxx";
    static final String TAG = "AndroidHive GCM";

    public static final String DISPLAY_MESSAGE_ACTION = "myPackage.pushNotifications.DISPLAY_MESSAGE";
    static final String EXTRA_MESSAGE = "message";
    static void displayMessage(Context context, String message)
        {
            Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
            intent.putExtra(EXTRA_MESSAGE, message);
            context.sendBroadcast(intent);
        }
}
Farrukh
  • 259
  • 2
  • 5
  • 15
  • 1
    Are your emulator (or real phone) the Google API's? Have you check http://stackoverflow.com/questions/11341305/android-gcm-gcmregistrar-gives-empty-registration-id? – sabadow Dec 24 '12 at 10:18
  • i am testing on real device 2.2 Froyo 7 inches tablet samsung – Farrukh Dec 24 '12 at 10:45
  • @Farrukh : Have u solved this problem.I am facing the same issue.Please help. – Shahzad Imam Jun 16 '13 at 05:21
  • http://stackoverflow.com/questions/13269055/gcm-push-messages-are-not-received/20631751#20631751 you can try this tutorial..it worked for me. – IAmNoob Dec 17 '13 at 10:40
  • http://stackoverflow.com/questions/13269055/gcm-push-messages-are-not-received/20631751#20631751 This tutorial helped me. – IAmNoob Dec 17 '13 at 10:41

3 Answers3

1

You should include your log output so that we know what happens when you run your app. You should also take a look at the GCM demo and use that as your base because you seem to be missing some important code: http://code.google.com/p/gcm/source/browse/samples/gcm-demo-client/src/com/google/android/gcm/demo/app/DemoActivity.java

If you get your registration ID from GCM then you are registered with GCM (are you getting a registration ID?) and the next step is to register with your server. This should happen in the else of the following If block:

if (regId.equals(""))

And in onRegistered in your GCM service as well. That is the callback that will be triggered when your app is successfully registered with GCM. Getting the registration ID right after attempting to register will not output anything since the registration with GCM is asynchronous.

selsine
  • 2,813
  • 2
  • 21
  • 22
1

If it is not already resolved .. I guess This could be the cause of problem... add this

 <uses-permission android:name="my_app_package.permission.C2D_MESSAGE" />

to your android manifest xml.

Hope it helps.

Relsell
  • 771
  • 6
  • 18
0

You have to enter your unique_ID(i.e. of 12 character) CommonUtilities.java

/**
* Google API project id registered to use GCM.
*/
static final String SENDER_ID = 123456789012; <----
MAC
  • 15,799
  • 8
  • 54
  • 95