3

I have tried some forum pages and searched through stackoverflow for the same thing, but I did not find the solution.

I have used the below code to generate registration ID for GCM notification. But I get an empty string as a registration ID:

GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this);

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

regId = GCMRegistrar.getRegistrationId(this);

Log.v("log", " register id oslo : " + regId);

if (regId.equals("")) {
    // replace this with the project ID
    GCMRegistrar.register(this, "718437041388");
    Log.d("info", GCMRegistrar.getRegistrationId(this));
} else {
    Log.d("info", "already registered as" + regId);
}

Please help me with this, tell me if I got anything wrong.

Thank you in advance, Nirav.

Charles
  • 50,943
  • 13
  • 104
  • 142
thedarkcoder
  • 395
  • 4
  • 11

5 Answers5

7

You should not get the registrationId just after GCMRegistrar.register(this, "718437041388");
In fact, do you have a correct GCMIntentService.java which receive the regId?
If yes, you can get the regId from the onRegistered inside GCMIntentService.java

 @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        Log.d("GCMIntentService", "in GCMIntentService");
        displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);
    }
jjLin
  • 3,281
  • 8
  • 32
  • 55
  • 1
    Hello jjLin, thanks for the quick response, but the problem is when registration of application, not when receiving the message. When we execute the code, it should get registration id as a string, but I got blank string instead. Please help me if you know the solution. thanks – thedarkcoder Jul 12 '12 at 09:57
  • 1
    Sorry, why you think the problem is when the registration? – jjLin Jul 12 '12 at 10:00
  • Actually, I am creating an notification application using GCM, So for that I need registration ID of the application. And that ID can be brought with the above code as we have tested and verify this code in another app and found working fine. So please tell me where can be the issue exactly? – thedarkcoder Jul 12 '12 at 10:07
  • You should place Log.d("info", GCMRegistrar.getRegistrationId(this)); outside the if else – jjLin Jul 13 '12 at 04:43
5

Notice that

    GCMRegistrar.register(this, "...");

is asynchronous. Therefore, calling GCMRegistrar.getRegistrationId(this) immediately after that is not reliable, so you should avoid it.

The id will arrive via broadcast callback to your GCMIntentService, as a part of the registration procedure. from there you can than store the gcm key anywhere you like, and use it in other parts of the application (usually on the server).

Notice that GCMIntentService should be defined at the root package of your app for it to work.

Amir Uval
  • 14,425
  • 4
  • 50
  • 74
1

Try using emulator with Google API and also sync with a Google account.

Echilon
  • 10,064
  • 33
  • 131
  • 217
Zubair
  • 436
  • 5
  • 13
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/faq#reputation) you will be able to [comment on any post](http://stackoverflow.com/privileges/comment). – bmargulies Nov 22 '12 at 12:59
  • Actually I had the same problem as he had and I got registration after doing this. – Zubair Nov 23 '12 at 04:19
0

I have same problem after 1 day struggle I find solution. First i Put my GCM Related Code in To My Main Package and make change according to My Package in androidManifest.xml

i give you simple example. My project name is GCMExample and i have Three package 1. com.examle.GCMExample(Main Package) , 2. com.examle.GCMExample.activity(Second Package) , 3. com.example.GCMExample.adapter(Third Package)

I am not Getting Registration Id When My GCM Related Class File Into My Second Package

My GCM Related class like 1. GCMIntentService , 2. ServerUtilities , 3. CommonUtilities 4. WakeLocker put Into My Main Package com.example.GCMExample

also my androidManifest.xml

         <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.GET_ACCOUNTS" />
         <uses-permission android:name="android.permission.WAKE_LOCK" />
         <uses-permission android:name="android.permission.VIBRATE" />

         <permission android:name="com.example.GCMExample.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />

         <uses-permission android:name="com.example.GCMExample.C2D_MESSAGE" />

       <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
   <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.GCMExample" />
          </intent-filter>
  </receiver>  

 <service android:name="com.example.GCMExample.GCMIntentService" />
Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42
0

Add GCM jar file in your project's lib folder