0

I followed the instructions here

Below is the code:

    if (checkPlayService()) {
        gcm = GoogleCloudMessaging.getInstance(context);
        regid = getRegistrationId(context);
        if (regid.isEmpty()) {
            registerInBackground();
        } else {
            Log.i(TAG, "No valid Google Play Services APK found.");
        }
    }
    private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String msg = "";
            if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(context);
            }
            try {
                Log.i(TAG, "Working in Background");
                Log.i(TAG, "RegId = "+regid);
                regid = gcm.register(SENDER_ID);
                Log.i(TAG, "RegId = "+regid);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                 msg = "Error :" + e.getMessage();
                 Log.i(TAG, msg);
            }
            msg = "Device Register with " + regid;
            Log.i(TAG, msg);
            sendRegistrationIdToBackend();
            // For this demo: we don't need to send it because the device
            // will send upstream messages to a server that echo back the
            // message using the 'from' address in the message.

            // Persist the regID - no need to register again.
            storeRegistrationId(context, regid);
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            registrationid = msg + "\n";
            Log.i(TAG, "Registration ID"+registrationid);
        }
    }.execute(null, null, null);
}


protected void storeRegistrationId(Context context, String regid) {
    // TODO Auto-generated method stub
    Log.i(TAG, regid);
}

protected void sendRegistrationIdToBackend() {
    // TODO Auto-generated method stub
    final SharedPreferences prefs = getGCMPreferences(context);
    int appVersion = getAppVersion(context);
    Log.i(TAG, "Saving regId on app version " + appVersion);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(PROPERTY_REG_ID, regid);
    editor.putInt(PROPERTY_APP_VERSION, appVersion);
    editor.commit();
}

public String getRegistrationId(Context context) {
    Log.i(TAG, "Inside getRegistrationId function");
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString("PROPERTY_REG_ID", "");
    if (registrationId.isEmpty()) {
        Log.i(TAG, "Registration not found.");
        return "";
    }
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
            Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion) {
        Log.i(TAG, "App version changed.");
        return "";
    }
    return registrationId;
}

private int getAppVersion(Context context) {
    // TODO Auto-generated method stub
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException("Could not get package name: " + e);
    }
}

private SharedPreferences getGCMPreferences(Context context) {
    // TODO Auto-generated method stub
    return getSharedPreferences(UserRegister.class.getSimpleName(),
            Context.MODE_PRIVATE);
}

public boolean checkPlayService() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    Log.i(TAG, "Check Play Services" + resultCode);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Log.i(TAG, "This device is not supported");
            finish();
        }
        return false;
    }

    return true;
}

When I am running the code I am getting the below Log cat values :

07-22 09:58:15.069: D/dalvikvm(32393): GC_CONCURRENT freed 127K, 3% free 6716K/6919K, paused 2ms+2ms
07-22 09:58:15.098: I/UserRegistration(32393): Check Play Services0

07-22 09:58:15.098: I/UserRegistration(32393): Inside getRegistrationId function

07-22 09:58:15.108: I/UserRegistration(32393): Registration not found.

07-22 09:58:15.108: I/UserRegistration(32393): Working in Background

07-22 09:58:15.108: I/UserRegistration(32393): RegId = 

07-22 09:58:15.108: I/UserRegistration(32393): Check Play Services0

07-22 09:58:15.108: I/UserRegistration(32393): Inside getRegistrationId function

07-22 09:58:15.108: I/UserRegistration(32393): Registration not found.

07-22 09:58:15.118: I/UserRegistration(32393): Working in Background

07-22 09:58:15.118: I/UserRegistration(32393): RegId =

The problem I am facing in the AsychTask when I put log.i above the regid = gcm.register(SENDER_ID); I am getting above log message if I am using the below code snippet:

        Log.i(TAG, "Working in Background");
                    regid = gcm.register(SENDER_ID);
                    Log.i(TAG, "RegId = "+regid);

Log cat:

07-22 10:10:55.748: W/dalvikvm(432): threadid=11: thread exiting with uncaught exception (group=0x2b542210)

07-22 10:10:55.748: W/dalvikvm(432): 
threadid=12: thread exiting with uncaught exception (group=0x2b542210)

Please help me I am not able to find out why this is happening? I am not able to generate the regid also.

I have modified the Catch a bit instead of IOException I have change it to Exception then I am getting the below logs

07-22 11:16:35.868: I/UserRegistration(2040): Check Play Services0

07-22 11:16:35.868: I/UserRegistration(2040): Inside getRegistrationId function

07-22 11:16:35.868: I/UserRegistration(2040): Registration not found.

07-22 11:16:35.868: I/UserRegistration(2040): Working in Background

07-22 11:16:35.878: I/UserRegistration(2040): Check Play Services0

07-22 11:16:35.878: I/UserRegistration(2040): Inside getRegistrationId function

07-22 11:16:35.878: I/UserRegistration(2040): Registration not found.

07-22 11:16:35.878: I/UserRegistration(2040): Error :null

07-22 11:16:35.878: I/UserRegistration(2040): Device Register with 

07-22 11:16:35.928: I/UserRegistration(2040): Working in Background

07-22 11:16:35.948: I/UserRegistration(2040): Error :null

07-22 11:16:35.948: I/UserRegistration(2040): Device Register with 

now it is going to other line but I am not able to get the RegId it is showing as null.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splitwise"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission 
    android:name="android.permission.INTERNET"></uses-permission>
<uses-permission 
    android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission 
    android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission 
    android:name="com.google.android.c2dm.permission.RECEIVE"></uses-permission>
<uses-permission 
    android:name="com.example.registration.permission.C2D_MESSAGE"
    android:protectionLevel="signature"></uses-permission>
<uses-permission android:name="com.example.registration.permission.C2D_MESSAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.registration.UserRegister"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity 
        android:name="com.facebook.LoginActivity"></activity>
     <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"></meta-data>
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <receiver
        android:name="com.example.registration.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.registration" />
        </intent-filter>
    </receiver>
    <service android:name="com.example.registration.GcmIntentService" />
</application>

Ishank Shah
  • 33
  • 1
  • 9

1 Answers1

0

This line is throwing exception:

 int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

Whenever you will try to run your application devices which don't support Google play services then it will throw Exception,

Devices which are not supporting Google play services: https://support.google.com/googleplay/answer/1727131?hl=en-IN https://developer.android.com/google/play-services/index.html

so you should use try catch for that method.

Final conclusion is that your device does not support Google Play Services or have disabled (need to install) it.

Look at this(Same problem on Kindle devices): GCM is not working on Kindle fire devices: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf

Community
  • 1
  • 1
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85