18

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.

Anil Kocabiyik
  • 1,178
  • 6
  • 17
  • 47
  • Please give more detail on your problem. What returned empty string? What were the logcat output after GCMRegistrar.register is called? – azgolfer Jul 30 '12 at 03:45
  • thank you @azgolfer it is solved but now i can't send regid and device id to servlet, they seems null at servlet. Do you have any code about it. – Anil Kocabiyik Jul 30 '12 at 11:56
  • I used first answer here: http://stackoverflow.com/questions/3505930/make-an-http-request-with-android I use extended AsyncTask. – Anil Kocabiyik Jul 30 '12 at 12:04
  • @azgolfer logcat output 08-03 09:24:21.974: V/GCMRegistrar(14590): Registering receiver 08-03 09:24:21.974: D/GCMRegistrar(14590): resetting backoff for com.example.ilkgcm 08-03 09:24:21.984: V/GCMRegistrar(14590): Registering app com.example.ilkgcm of senders "my_sender_id" 08-03 09:24:36.248: A/libc(14590): Fatal signal 6 (SIGABRT) at 0x0000012f (code=0) – Anil Kocabiyik Aug 03 '12 at 06:26
  • when GCMRegistrar.register is called, logcat output as above. Fatal signal appears in 10-15 seconds. – Anil Kocabiyik Aug 03 '12 at 06:31
  • @user1451549 If your question was answered you should accept the answer... – Amir Uval Sep 20 '12 at 20:27

6 Answers6

49

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).

Amir Uval
  • 14,425
  • 4
  • 50
  • 74
  • for the callback? It's quite immediate. It's involving a communication with a system process, and the callback is coming from there. If you wait and it doesn't arrive it means something is wrong in your setup – Amir Uval Aug 02 '12 at 19:39
  • is it necessary to place the class in root package? – stinepike Jun 24 '13 at 07:15
  • 3
    It appears so, though it's not documented. My first attempt with GCM was not on the default package, and it failed. It worked when moved to root. – Amir Uval Jun 24 '13 at 11:18
  • Thanks, it helped alot. It's not documented but as per your suggestion GCM class should be on root package. – surhidamatya Nov 25 '13 at 08:12
11

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.

EvanBlack
  • 759
  • 8
  • 25
  • Thanks...Exactly first time not getting regId is empty and after then again lunch this apps we would get the regID but this steps follow then I would first time apps lunch and get this gcm token..!!!:) – Najib.Nj Apr 28 '14 at 05:46
5

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
  • Patel Dhaval, your magical answer save my day but this is very strange :) Thanks for your great help! (Thumbs up) – Saad Bilal Dec 24 '14 at 10:52
2

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.

divanshu
  • 335
  • 1
  • 5
  • 12
1

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.

1

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.

Twinsens
  • 417
  • 6
  • 23