user2291144
user2291144

Reputation: 65

GCMRegistrar.getRegistrationId(this) always returns empty string (Android Push notification using GCM)

 public void registerClient()
 {
  try
  {
   // Check that the device supports GCM (should be in a try / catch)
   GCMRegistrar.checkDevice(this);

   // Check the manifest to be sure this app has all the required
   // permissions.
   GCMRegistrar.checkManifest(this);

   // Get the existing registration id, if it exists.
   regId = GCMRegistrar.getRegistrationId(this);

   if (regId.equals(""))
   {

    // register this device for this project
    GCMRegistrar.register(this, PROJECT_ID);
    regId = GCMRegistrar.getRegistrationId(this);

regId = GCMRegistrar.getRegistrationId(this); always returns an empty string. Has GCMRegister class been deprecated? I have added gcm jar from sdk/extras/google/gcm/gcm-client path. i have also tried added google play service library and execute the code(removed gcm.jar) but it then says GCMRegistrar class not found exception I have check all permissions and they seem to be right. here is my manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gcmclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <!-- receives GCM messages -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google services -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />

    <!-- wake the processor if a GCM message is received -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission
        android:name="com.example.gcmclient.permission.C2D_MESSAGE"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.example.gcmclient.permission.C2D_MESSAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.example.gcmclient" />

                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.example.gcmclient" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" >
        </service>
    </application>

</manifest>

Upvotes: 1

Views: 3757

Answers (3)

Piyush Agarwal
Piyush Agarwal

Reputation: 25858

I you are using GCM please put these check before calling GCM registration method :

A properly synced Google account is mandatory for Android 4.0.4 to register with GCM so you can write something like this

 private boolean isGoogleAccountRequired=true;
 private Account[] accounts;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.sample);

   String firmwareVersion=Build.VERSION.RELEASE;
   String firmwareVersionsStrings[]=firmwareVersion.split("\\.");

   int firstDigit=Integer.valueOf(firmwareVersionsStrings[0]);
   int secondDigit=Integer.valueOf(firmwareVersionsStrings[1]);
   int thirdDigit=0;

   if(firmwareVersionsStrings.length>=3){
    thirdDigit=Integer.valueOf(firmwareVersionsStrings[2]);
   }

   if(firstDigit>=4){
  if(secondDigit>0){
    isGoogleAccountRequired=false;
  }else if(secondDigit==0){
    if(thirdDigit>=4){
       isGoogleAccountRequired=false;
    }
  }
   }


   if(isGoogleAccountRequired && !GCMRegistrar.isRegistered(this)){
    boolean isGoogleAccountPresent=isGoogleAccountPresent(this);

        // Check whether Google Account Present or not if not act accordingly
        if(! isGoogleAccountPresent){
    // Show Dialog to add google account and sync it
        }else{

           //Check if the account added is synced or not if not sync it programatically
           boolean syncEnabled=false;
           if(accounts.length>0){
             syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);   
       }

           if(!syncEnabled){
            ContentResolver.setSyncAutomatically(accounts[0], ContactsContract.AUTHORITY, true);
       }
             GCMRegistrar.register(YourActivity.this,SENDER_ID);

        }
   }else if(!GCMRegistrar.isRegistered(this)){
        GCMRegistrar.register(YourActivity.this,SENDER_ID);

   }
 }

Now in the GCMIntentService override these methods :

   @Override
   protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
   Log.i(TAG, "device is registered to GCM server with Reg ID In GCMINTENT::::"+ arg1);
   }

   protected void onMessage(Context context, Intent intent) {

   }

   @Override
   public void onError(Context context, String errorId) {
   Log.i(TAG, "Received error: " + errorId);
   }

Please check the logs error ID in onError Message if you still not getting the Reg Id.

Add this permission for Writing sync Settings :

    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>

Upvotes: 0

Hardik
Hardik

Reputation: 17441

your code look like ok, but when you use GCM you must sync account from setting. follow the step.

GO To SETTING ---> Add Account--->google

Upvotes: 1

NickT
NickT

Reputation: 23873

They deprecated the old method of doing GCM too quickly after its introduction in my opinion.

However it appears that you are mixing up code from the old method and the new Play Services implementation. You are probably best advised to start again using this Play Services example GCM client as a starting point

Upvotes: 0

Related Questions