Anil Kocabiyik
Anil Kocabiyik

Reputation: 1188

I can not get registration ID from Android GCM

Although I try other answer about this problem, it can't be solved. Sender ID is correct, permissions are added, API key is true.

I use this post for creating the project: http://developer.android.com/guide/google/gcm/gs.html#server-app

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

it returns empty string.

Upvotes: 18

Views: 44824

Answers (6)

Amir Uval
Amir Uval

Reputation: 14873

Do you have GCMIntentService defined correctly at the root package of your app?

<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="my_app_package" />
  </intent-filter>
</receiver>

Make sure that "my_app_package" is identical to the main package of your app, or your id will return an empty string.

Also 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 then store the gcm/fcm key anywhere you like, and use it in other parts of the application (usually on the server).

Upvotes: 49

EvanBlack
EvanBlack

Reputation: 759

If your application launches first time, you have to wait for OnRegistered callback on your GCMIntentService.java file.

GCMRegistrar.register(this, SENDER_ID);
// Android does NOT wait for registration ends and executes immediately line below
// So your regId will be empty.
regId = GCMRegistrar.getRegistrationId(this);

GCMIntentService.java:

@Override
protected void onRegistered(Context c, String regId) {
    // You can get it here!
}

Edit: Newer version of GCM library (which bundled with google play services library) waits for response and returns the Registration ID. So this answer is for older GCM libraries.

Upvotes: 11

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: 5

Twinsens
Twinsens

Reputation: 447

For example your receiver class name is : "MyCustomIntentService.java" , just change this name as "GCMIntentService.java" then try it again. This is simple solution. Just change your file name on the SRC area.

Upvotes: 1

Vinoth Prakash K
Vinoth Prakash K

Reputation: 11

If you are not getting response from the registration.one of the main reason is your manifest file is not configured correctly...especially give the "package_name"(your app package name like com.xxx.yyy) in the and correctly.

Upvotes: 1

divanshu
divanshu

Reputation: 345

Registering app com.example.ilkgcm of senders "my_sender_id" -- This error suggests you haven't changed your 'my_sender_id' to a valid sender id which would be a 12-letter code.

Upvotes: 2

Related Questions