Dor Mesica
Dor Mesica

Reputation: 527

GCM SERVICE_NOT_AVAILABLE error

I'm using GCM in my android application and when I try to register the device with GCM (with the command gcm.register(SENDER_ID)) I get the SEVICE_NOT_AVAILABLE error, I tried what someone suggested here (the accepted solution) and I did get the registration id.

Why is it like that?

Android manifest:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <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" />

    <permission
        android:name="com.appspot.smartgan.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name="com.appspot.smartgan.MainActivity"
            android:label="@string/app_name" >
            <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" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.appspot.smartgan" />
            </intent-filter>
        </receiver>

        <service 
            android:name=".GcmIntentService"
            android:enabled="true" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.appspot.smartgan.LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="adjustResize|stateVisible" >
        </activity>
        <activity
            android:name="com.appspot.smartgan.ChildActivity"
            android:label="@string/title_activity_child" >
        </activity>
    </application>

</manifest>

register method:

private void registerInBackground() {   
    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            String message = "";

            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                message = "Device registered, registration ID=" + regid;

                Log.d("SMARTGAN_PLAY", "register completed");

                // send registration id to the server
                sendRegistrationIdToServer();

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException e) {
                message = "Error:" + e.getMessage();
            }
            return message;
        }

    }.execute(null, null, null);
}

Upvotes: 1

Views: 2396

Answers (2)

MarcD
MarcD

Reputation: 65

If you are receiving a Register ID it means you have registered the device successfully to the GCM.

Edit: Here is a good tutorial for GCM: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

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

    <service android:name="com.google.android.gcm.GCMIntentService" android:enabled="true" />

My GCM is set up based on the Tutorial i linked above so if you read through that aswell it should work for you it looks like your not far off. I will say the Manifest is very picky and you need to ensure that you link your Packages fully.

Also you dont need the Register Intent

Upvotes: 0

SaurabhJinturkar
SaurabhJinturkar

Reputation: 554

I think issue is regarding allowed IP addresses mentioned in your Google API project. Please check details of the project and remove IP address if present under restrict use to IP address. I faced similar problem.

Upvotes: 2

Related Questions