0

I am trying to integrate GCM in my android project. I have created project on Google API Console and enabled the GCM for Android Service.

When I try to register it then I am getting exception java.io.IOException: SERVICE_NOT_AVAILABLE. I am using correct SENDER_ID that is Project Number in API Console.

Can anyone help me what is the cause of this problem ?

My time of mobile is correct and below is the things I added in AndroidManifest.xml

Android.xml

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

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

    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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

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

    <application
        android:name="com.xyz.application.LaddooApplication"
        android:allowBackup="true">

        <receiver
            android:name="com.xyz.gcm.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

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

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

    </application>

</manifest>

Code

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                int retry = 0;
                while (retry < 5 && regid.length() == 0) {
                    regid = gcm.register(SENDER_ID);
                    ++retry;
                }

                msg = "Device registered, registration ID=" + regid;

                if (!regid.equals("")) {
                    // You should send the registration ID to your server
                    // over HTTP, so it
                    // can use GCM/HTTP or CCS to send messages to your app.
                    sendRegistrationIdToBackend();
                }
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
        }
    }.execute(null, null, null);
}

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

3 Answers3

0

Seems like you haven't defined a Service to handle the GCM message. Check this link for details. It should look like this in your Manifest: <service android:name="com.xyz.gcm.GcmIntentService" />

jmm
  • 1,044
  • 2
  • 12
  • 38
  • In other answers, like [this](http://stackoverflow.com/a/25183437/3923525) or [this](http://stackoverflow.com/a/17189160/3923525) people suggest to sleep the Tread for a little while and retry afterwards. Have you tried that? – jmm Dec 01 '14 at 15:26
0

While I was debugging my code for GCM registration i noticed that Google Play services are not happy to receive this call very often.

It always works if you delete an app and try calling this method after a fresh install.

I followed guidelines from here and all seems to work OK now: https://developer.android.com/google/gcm/client.html

virtas
  • 53
  • 8
0

If you are running your app in emulator, you must install Google Play Service.

I recently switched to GenyMotion Emulator from Google native emulator and encountered this error. Later I realized GenyMotion image does not come with Google Play Service. After installation, problem solved.