1

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);
}
Community
  • 1
  • 1
Dor Mesica
  • 507
  • 1
  • 7
  • 23

2 Answers2

2

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.

SaurabhJinturkar
  • 554
  • 7
  • 20
0

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

MarcD
  • 65
  • 8
  • I know but when the `gcm.register()` yields an IOException – Dor Mesica Dec 18 '13 at 16:19
  • I really cant help you to much without seeing a copy of your code so i know exactly where your placing your register command. Ive edited my answer with a link to a tutorial that helped me to get mine to work. If you can edit your question with your code ill be able to help more. – MarcD Dec 19 '13 at 11:08
  • the activity class in which the register function is called is very long so I can't add everything here, but I followed the sample project Google published [here](http://developer.android.com/google/gcm/client.html) so you can see my code there it's the same. – Dor Mesica Dec 19 '13 at 14:48
  • Honestly mate we really cant help you unless we see some code we are just stabbing in the dark at the moment. i get the feeling it could be a manifest thing tbh all my problems stemmed from incorrectly setting up the receiver in the manifest. – MarcD Dec 19 '13 at 16:38
  • I've added both the manifest and the registerInBackground function – Dor Mesica Dec 19 '13 at 17:07
  • Sorry for the wait in a reply it was christmas and i wasnt in my office ive updated my answer with an example of a manifest receiver code. – MarcD Jan 09 '14 at 10:24