Thomas Besnehard
Thomas Besnehard

Reputation: 2095

Google Cloud Messaging - GCM - SERVICE_NOT_AVAILABLE

I am trying, to implement the new GCM, I am following Google get stated : http://developer.android.com/guide/google/gcm/gs.html

I getting stuck at getting my device registration ID !

My app keep trying to connect with Google server, Here my error logs :

onReceive: com.google.android.gcm.intent.RETRY
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
Registering app com.dombox.app of senders _my_sender_id_
Releasing wakelock
onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Registration error: SERVICE_NOT_AVAILABLE
Scheduling registration retry, backoff = 912617 (768000)
Releasing wakelock

Here is my Activity Code, asking for an ID :

    try{
        Log.i(TAG, "[checkNotifRegistration] checkDevice");
        GCMRegistrar.checkDevice(this);
        Log.i(TAG, "[checkNotifRegistration] checkManifest");
        GCMRegistrar.checkManifest(this);
        if (GCMRegistrar.isRegistered(this)) {
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        }
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            // SENDER_ID is my project id into google account url
            GCMRegistrar.register(this, SENDER_ID);
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        } else {
            Log.i(TAG, "[checkNotifRegistration] already registered as : " + regId);
        }
    } catch(Exception e){
        Log.e(TAG, "[checkNotifRegistration] Exception : "+e.getMessage());
        e.printStackTrace();
    }

Here my Service code

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
           // SENDER_ID is my project id into google account url
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start");
}


public GCMIntentService(String senderId) {
    super(senderId);
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start - sender Id : "+senderId);
}
}

And here is my Android Manifest :

<permission android:name="com.dombox.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.dombox.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

    <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.myapp.app" />
        </intent-filter>
    </receiver>

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

    <activity
        android:name=".activity.Myapp"
        android:label="@string/app_name" >

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

    </activity>

I'm following Google's instruction, but I stuck with this SERVICE_NOT_AVAILABLE error,

What am I doing wrong ?

Upvotes: 23

Views: 27411

Answers (6)

kelvincer
kelvincer

Reputation: 6138

I had this problem running my application on genymotion. Installing google play services like it says here and everything worked fine.

Upvotes: 1

Vin&#237;cius Barbosa
Vin&#237;cius Barbosa

Reputation: 21

I've had this problem and it was caused by the WIFI connection. I turned WIFI off and made the test again (on 3g) and it Worked. Then I turned the WIFI back on and had the error again. Back to 3g, to make sure, and it worked again.

So, in conclusion, the issue was caused by the connection.

Upvotes: 2

Ryan Gray
Ryan Gray

Reputation: 824

android:enabled="true"  

Remove this ^

If you don't already add this to your manifest

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8"

You don't need the second constructor in GCMIntentService. only this one

public GCMIntentService() {         
 super(SENDER_ID);  }

Ensure that your SENDER_ID is the number copied out of the URL from your browser while you are browsing the google code website AND browsing the GCM service.

Upvotes: 7

Daniel
Daniel

Reputation: 1799

Remember that if you're behind a corporate firewall, some ports that GCM uses may not be allowed to communicate over the local wireless. I had this problem when trying to get my emulator to work initially. I had to get a special IP that allowed the ports listed below through.


Note: If your organization has a firewall that restricts the traffic to or from the Internet, you need to configure it to allow connectivity with GCM in order for your Android devices to receive messages. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs, so you should allow your firewall to accept outgoing connections to all IP addresses contained in the IP blocks listed in Google's ASN of 15169.

http://developer.android.com/google/gcm/gcm.html

Upvotes: 3

Thomas Besnehard
Thomas Besnehard

Reputation: 2095

The problem was not code related, but link to the test phone. The test phone do not have a SIM, and the clock was not set. So google cannot resgistred it with a wrong time.

I manage to have a registration Id using a Android Virtual Device, with google api, and by setting the test phone clock.

Upvotes: 39

jpCoder411
jpCoder411

Reputation: 207

I had this problem...my solution was to disable internet connection and use the phone service.

Upvotes: 2

Related Questions