30

I encountered a strange problem - I've been using GCM in my application for quite a long time and everything works perfectly. However, before a release to Google Play I changed my application package name from com.android.testapp to com.android.recognition and after this GCM stopped working. At first I got en error GCM sender id not set on constructor and fixed it by overriding getSenderIds(Context context), but now I can't get a registration ID. Here are the messages from logcat: enter image description here

How can I fix this? When I switched to a new package I changed everything in the manifest file to the new package:

<receiver
        android:name="com.google.android.gcm.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.android.recognition" />
        </intent-filter>
    </receiver>

So what is the problem behind this? Can renaming the application package cause this or is there another reason?

4
  • Are you working on emulator? Which emulator? Commented Jun 19, 2013 at 10:39
  • @PankajKumar I'm working on real devices: Kindle Fire and LG NEXUS 4.And nothing works Commented Jun 19, 2013 at 11:28
  • For me, I look strange. I have to try a few times. Sometimes, I need to force close the app. Then It works.
    – K.Sopheak
    Commented Nov 11, 2016 at 7:34
  • just check your internet is working fine or not... FCM needs the internet on the running device to hit the web service and generate a device token. Commented Oct 10, 2019 at 11:02

17 Answers 17

45

The problem is answered, in my case it was little more complicated.

  1. Check you have an active internet connection
  2. Check that you have Internet permission in your manifest
  3. Make sure the package name is correct as Eran mentioned
  4. The device time is correctly setup. Even if everything is perfect, it will fail if device clock is not set correctly.

Wrong clock caused problem for me. :)

4
  • Yep, I dropped wifi connection, and didn't realize it. Thanks.
    – nland
    Commented Apr 1, 2016 at 19:26
  • I'd add to check if Google Play store is working, if it isnt there is some problem in the google account the device is using that may cause this behaviour. Also, check google play services is updated.
    – Ofek Ron
    Commented Sep 4, 2016 at 9:47
  • Great point! Can you please update the answer with some snippet, may be? Commented Sep 4, 2016 at 16:56
  • System time... dammit
    – zozelfelfo
    Commented Sep 10, 2018 at 17:06
34

This SERVICE_NOT_AVAILABLE error says that GCM Service is not available in current. Wait and try after some time.

This happens many time (As my experience), so don't worry about it.


See the GCMConstants class of GCM Lib.

/**
     * The device can't read the response, or there was a 500/503 from the
     * server that can be retried later. The application should use exponential
     * back off and retry.
     */
    public static final String ERROR_SERVICE_NOT_AVAILABLE =
            "SERVICE_NOT_AVAILABLE";

For more investigation see handleRegistration() of GCMBaseIntentService

private void handleRegistration(final Context context, Intent intent) {
        String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
        String error = intent.getStringExtra(EXTRA_ERROR);
        String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
        Log.d(TAG, "handleRegistration: registrationId = " + registrationId +
                ", error = " + error + ", unregistered = " + unregistered);

        // registration succeeded
        if (registrationId != null) {
            GCMRegistrar.resetBackoff(context);
            GCMRegistrar.setRegistrationId(context, registrationId);
            onRegistered(context, registrationId);
            return;
        }

        // unregistration succeeded
        if (unregistered != null) {
            // Remember we are unregistered
            GCMRegistrar.resetBackoff(context);
            String oldRegistrationId =
                    GCMRegistrar.clearRegistrationId(context);
            onUnregistered(context, oldRegistrationId);
            return;
        }

        // last operation (registration or unregistration) returned an error;
        Log.d(TAG, "Registration error: " + error);
        // Registration failed
        if (ERROR_SERVICE_NOT_AVAILABLE.equals(error)) {
            boolean retry = onRecoverableError(context, error);
            if (retry) {
                int backoffTimeMs = GCMRegistrar.getBackoff(context);
                int nextAttempt = backoffTimeMs / 2 +
                        sRandom.nextInt(backoffTimeMs);
                Log.d(TAG, "Scheduling registration retry, backoff = " +
                        nextAttempt + " (" + backoffTimeMs + ")");
                Intent retryIntent =
                        new Intent(INTENT_FROM_GCM_LIBRARY_RETRY);
                retryIntent.putExtra(EXTRA_TOKEN, TOKEN);
                PendingIntent retryPendingIntent = PendingIntent
                        .getBroadcast(context, 0, retryIntent, 0);
                AlarmManager am = (AlarmManager)
                        context.getSystemService(Context.ALARM_SERVICE);
                am.set(AlarmManager.ELAPSED_REALTIME,
                        SystemClock.elapsedRealtime() + nextAttempt,
                        retryPendingIntent);
                // Next retry should wait longer.
                if (backoffTimeMs < MAX_BACKOFF_MS) {
                  GCMRegistrar.setBackoff(context, backoffTimeMs * 2);
                }
            } else {
                Log.d(TAG, "Not retrying failed operation");
            }
        } else {
            // Unrecoverable error, notify app
            onError(context, error);
        }
    }
5
  • 4
    "Wait and try after some time." I have this error for three days already on different devices Commented Jun 19, 2013 at 11:29
  • 2
    I didn't face this issue over 1 day. If you are facing this issue for 3 days, it might some other problem too.. Did you get success ever on same code in prev? Commented Jun 19, 2013 at 11:32
  • " I use GCM in my application for quite a long time and everything works perfectly" I wrote about it in my question Commented Jun 19, 2013 at 11:37
  • 1
    Hey did you change your package name on GCM project page? Commented Jun 19, 2013 at 12:06
  • 1
    Maybe someone can share his code of handling this error, but with the new GCM API? Since this code here is deprecated.. Thanks
    – MichalK
    Commented Nov 10, 2013 at 8:35
24

SERVICE_NOT_AVAILABLE is one of the most frustrating problems with Google Cloud Messaging. It is an exception thrown by GoogleCloudMessaging.register(SENDER_ID), the function call that registers the device for push notifications and returns a registration ID.

  1. SERVICE_NOT_AVAILABLE might mean that the user’s device can’t read the response to the registration request or a 500/503 error code was returned from the server. Developers have no way to fix this error because it is on Google’s end, so we can blindly suggest that the user should try again in a few hours.
  2. SERVICE_NOT_AVAILABLE may occur on some devices even though the registration succeeded. This can be fixed by implementing a workaround broadcast receiver to catch the token when the call fails. I implemented this workaround and it may have fixed the problem for some users, but still I received many other SERVICE_NOT_AVAILABLE complaints.
  3. SERVICE_NOT_AVAILABLE may occur because of an outdated or missing Google Play Services library on the device. In this case, the app could theoretically notify the user to update Google Play Services by opening the respective Google Play app listing. However, the app has no idea that this is why SERVICE_NOT_AVAILABLE was thrown, so it cannot blindly redirect the user to the Google Play Services app page on Google Play.
  4. SERVICE_NOT_AVAILABLE may occur when the device’s clock is not synchronized with the network. Again, developers have no way of knowing that this is the exact problem, so we can blindly suggest to the user to check their system clock synchronization, hoping they are one of the very few whose clocks are not synchronized.
  5. SERVICE_NOT_AVAILABLE may occur when a rooted user has deleted the Hangouts/GTalk app from their device (because they considered it bloatware). GCM is implemented and handled by Hangouts/GTalk, so it is not possible to use GCM without it.
  6. SERVICE_NOT_AVAILABLE may occur if the user is running a device that does not have Google APIs installed (such as the Amazon Kindle). Nothing to do here, these users will never receive push notifications from your app.

Read more: http://eladnava.com/google-cloud-messaging-extremely-unreliable/

These issues alone were enough to get me to start looking for GCM alternatives. I’d get a 1-star review on my app every day or two, with a comment containing the error message displayed when a SERVICE_NOT_AVAILABLE was thrown. There was nothing I could do to help these users, because the majority of them were receiving it for reasons out of their control.

An Alternative to Google Cloud Messaging

Pushy (https://pushy.me/) is a standalone push notification gateway, completely independent of GCM. It maintains its own background socket connection, just like GCM, to receive push notifications. The underlying protocol is MQTT, an extremely light-weight pub/sub protocol, utilizing very little network bandwidth and battery.

A huge advantage of Pushy is that the code for sending a push notification (from the server), and registering the device for push notifications, is actually interchangeable between GCM and Pushy. This makes it super easy to switch to Pushy after implementing GCM and having to ditch it for its instability.

(Full disclosure: I founded Pushy for my own projects and realized many apps would benefit from such a service)

2
  • Can pushy.me tell me when the device has an active connection to your sever and when its disconnected. I want to only send push to connected devices.
    – akshay202
    Commented Jan 2, 2016 at 10:56
  • Absolutely! Our Device Info API provides the device's last_active device presence: pushy.me/docs/api/device
    – Elad Nava
    Commented Dec 20, 2016 at 15:12
18

Make sure that you changed the package name in the permissions part of your manifest :

<permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" />

I had a similar error due to an incorrect package name in that part.

5
  • Isn't it sufficient to use <permission android:name=".C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name=".C2D_MESSAGE" /> if you have the package attribute in the manifest xml? Commented Jun 17, 2015 at 6:16
  • @user2104070 No, the permission must contain the package name.
    – Eran
    Commented Jun 17, 2015 at 6:42
  • so does that mean if I have different builds (in Android Studio) I would need to maintain separate AndroidManifest versions for each (as the package names would be different)? Commented Jun 17, 2015 at 8:08
  • @user2104070 Doesn't each version having a different package name already requires a different AndroidManifest.xml (since the package is specified in the manifest)? I'm not familiar with Android Studio. If the package is inserted automatically to the manifest during the build process, perhaps there's a way to insert the package specific C2D_MESSAGE permission during the build as well.
    – Eran
    Commented Jun 17, 2015 at 9:12
  • If you're using Gradle you can use "${applicationId}.permission.C2D_MESSAGE" to support variable package names by build type or flavor. Commented Feb 18, 2016 at 19:45
13

For me - the device time wasn't correct. I changed the device settings to use "Automatic date & time", tried again and all good.

Cheers

1
  • 2
    To hell with downvoters, this is what was the problem in my case too. Commented Mar 4, 2015 at 8:44
3

I had the same problem but none of the above solutions solved the problem in my case. Fortunately I recently solved it and I want to explain how, hopping it will help others:

In my case, I was registering the push service in a custom application class (which is executed before any activity and I think is due to this that some things have not been initialized propertly). Changing it to the main activity solved the problem.

public class MyCustomApp extends Application {

    @Override
    public void onCreate() {
         super.onCreate();
         PushService.register(this); //BAD IDEA, don't register pushes in Application Class
    }

}
3

I had a similar problem. Worked fine on a google nexus (Android 4.4.2) but not on a Samsung galaxy s3 (Android 4.1.2). I was getting SERVICE_NOT_AVAILABLE on registration on the Samsung. It turned out the time on Samsung was off. It was not set to auto update with Network Time. Once I fixed that GCM worked like a charm. Thanks - Umesh

3

For me there was connection problem. Change internet connection solved my problem

1
  • 1
    This solved my problem as well. I think it's a good hint that you should make sure your network is working when you see the SERVICE_NOT_AVAILABLE error. Commented Mar 5, 2015 at 3:20
3

For me, I had turned of "Background Data Access" for google services by checking "Restrict background data" in Data usage option on my Galaxy S4. As soon as I turned it on problem resolved on cellarer network. On Wifi it was working fine.

1
  • 1
    And for me the problem was that I didn't connect to the local WIFI. I know this sounds dumb but what I'm trying to say is that networking can be the problem. Commented Mar 5, 2015 at 3:18
3

In my case, the solution was to add a new intent-filter action, REGISTRATION, to the manifest, per https://snowdog.co/blog/dealing-with-service_not_available-google-cloud-messaging/

    <receiver
        android:name=".RemoteNotificationReceiver"
        android:permission="com.getset.getset.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.getset.getset.c2dm.intent.RECEIVE" />
            <action android:name="com.getset.getset.c2dm.intent.REGISTRATION" />
            <category android:name="com.getset.getset" />
        </intent-filter>
    </receiver>

I have to admit that I'm surprised that this works, given it's missing from the tutorial, but taking it out definitely turns a successful registration id into an exception.

Note: using Nexus 5 API 21 (Lollipop) emulator.

1
2

For me, the problem was the phone was not connected to the internet. I disconnect and connect to Wi-Fi, and tested connectivity with Browser and tested again. Worked like a charm :-)

0

I had turned of "Background Data Access" for google services. By unchecking "Restrict background data" in Data usage option It works for me !

0

For me goolge blocked my IP!! I had to reset my DSL conn to get a new IP from the pool and everything worked again, idk why they blocked me, maybe for trying many apps? Anyway is working now, i hope this help somebody else :)

0

For me,SERVICE_NOT_AVAILABLE problem was in my application project because of the receiver class. So I solved after implementing the receiver like the following. <receiver android:name="receiver name" 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="your package"/> </intent-filter> </receiver> I hope that will help you :-) .

0

I had a OnePlus2 which couldn’t receive pushes when on data. When I connected with logcat, I saw a lot of this error, but am unsure if it is related.

I gave up trying to find a setting which corresponded to it and just factory reset the device. OnePlus devices running OxygenOS sometimes get weird configuration errors when installing software updates and a factory reset followed by restore from Google Backup gets things working again faster than it would take to understand the underlying issue (and it might be that the user doesn’t even have the right access to fix the underlying issue).

0

In my case, this happened because my mobile phone does not have a SIM card inside it.

I guess that having the sim card sets the Automatic time on the device, which resolves this.

-1

After a long struggle i managed to sort out this issue. Make sure that Google Play Services app is up-to-date and that its background synchronization is not disabled on your phone.

Not the answer you're looking for? Browse other questions tagged or ask your own question.