Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 247
Anil Kocabiyik Avatar asked Jul 29 '12 22:07

Anil Kocabiyik


3 Answers

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

like image 189
Amir Uval Avatar answered Oct 16 '22 08:10

Amir Uval


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.

like image 42
EvanBlack Avatar answered Oct 16 '22 08:10

EvanBlack


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" />
like image 36
Dhaval Jivani Avatar answered Oct 16 '22 10:10

Dhaval Jivani