thedarkcoder
thedarkcoder

Reputation: 395

App registration ID not generating in GCM Android

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.

Upvotes: 3

Views: 9384

Answers (5)

Gurpreet Singh Turka
Gurpreet Singh Turka

Reputation: 11

Add GCM jar file in your project's lib folder

Upvotes: 0

Dhaval Jivani
Dhaval Jivani

Reputation: 9697

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

Upvotes: 0

Zubair
Zubair

Reputation: 436

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

Upvotes: 1

Amir Uval
Amir Uval

Reputation: 14873

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.

Upvotes: 5

jjLin
jjLin

Reputation: 3291

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);
    }

Upvotes: 7

Related Questions