简体   繁体   中英

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.

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

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.

尝试将模拟器与Google API配合使用,并与Google帐户同步。

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

在项目的lib文件夹中添加GCM jar文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM