Shahriar
Shahriar

Reputation: 824

Android gcm client using GoogleCloudMessaging

I am using the new way of using gcm described here using GoogleCloudMessaging class. But 9 out of 10 times it is giving me SERVICE_NOT_AVAILABLE error. Only once it registers a device.

07-01 14:24:55.907    2074-2090/com.deveyes.carrefour          W/System.err:                   java.io.IOException: SERVICE_NOT_AVAILABLE

07-01 14:24:56.037    2074-2090/com.deveyes.carrefour          W/System.err: at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)

My code is as follows inside my Activity:

 .....
     gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID);   
 .....                                                       

Later I came to know that I need to use Automatic retry. I have read this but this document seems to be old. As with the new way we do not use IntentService anymore. or do we ? Inside BroadcastReciever i can get the message and notify user.

what I am doing wrong here?

EDIT:

MY manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />


<supports-screens
        android:xlargeScreens="false"
 />


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

<uses-feature android:name="android.hardware.location" android:required="true" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />

<permission
        android:name="com.deveyes.carrefour.android.MAPS_RECEIVE"
        android:protectionLevel="signature" />


<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

<uses-permission android:name="com.deveyes.carrefour.android.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Carrefour"
        android:name=".CarrefourApplication">

    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCKznwauzk_rNdKf7Qw0rjpmf4AWchCRwI" />

    <activity
        android:name=".ui.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


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

    <!--service android:name=".services.CarrefourLocationService" /-->


</application>

My MainActivity:

private void registerBackground() {

    new AsyncTask(){

        @Override
        protected Object doInBackground(Object[] objects) {

            try {
                if(gcm == null){
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                 gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID);

                Log.i(TAG,"regid:"+gcmRegId);

                setRegistrationId(context,gcmRegId);
                ServerUtilities.register(context,gcmRegId);


            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute(null,null,null);
}

Upvotes: 1

Views: 7033

Answers (3)

tony9099
tony9099

Reputation: 4727

I had the same problem.

9/20 throws the SERVICE_NOT_AVAILABLE exception. Apparently it is a bug in the Google's side. Works on some devices (S3) and does not on others (LG L5).

Check Here.

I think the thing to do is use the old API temporarily until the issue is resolved.

Upvotes: 0

Vardhan D G
Vardhan D G

Reputation: 316

hey in the GCM jar there should be something called GCMRegistrar you should use that method to register to GCM. following is the code, try it

GCMRegistrar.checkDevice(this);
 GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
                DISPLAY_MESSAGE_ACTION));

        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
             GCMRegistrar.register(this, SENDER_ID); //this is where it registers
        } else {

            if (GCMRegistrar.isRegisteredOnServer(this)) {
                   Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
            } 

i hope it works for you

Upvotes: 1

Eran
Eran

Reputation: 394086

Here's the problem :

You should use the same package name in all 3 places below, but you use com.deveyes.carrefour.android in some cases and com.deveyes.carrefour in other cases.

<permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" />
...
    <receiver
            android:name=".gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.deveyes.carrefour" />
        </intent-filter>
    </receiver>

If your package name is com.deveyes.carrefour, change the manifest to :

<permission android:name="com.deveyes.carrefour.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="com.deveyes.carrefour.permission.C2D_MESSAGE" />
...
    <receiver
            android:name=".gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.deveyes.carrefour" />
        </intent-filter>
    </receiver>

Upvotes: 2

Related Questions