11

I am setting up Google Cloud Messaging in an Android application. At the beginning, I initialise the GoogleApiClient in order to check whether the Play Services are available:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

Trying to run it produces IllegalArgumentException: must call addApi() to add at least one API, so I also need to add the GCM Api, but honestly I can't find it on the documentation. Something like:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(gcm.API)     <----- WHAT HERE?
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
jww
  • 97,681
  • 90
  • 411
  • 885
ticofab
  • 7,551
  • 13
  • 49
  • 90
  • 1
    AFAIK you **don't need** `GoogleApiClient` for GCM – Selvin Jun 17 '15 at 09:58
  • 1
    It isn't strictly needed, but the documentation "strongly encourages" to use it in order to check whether the Play Services are available or not: https://developers.google.com/android/guides/setup – ticofab Jun 17 '15 at 10:00
  • 1
    you can also use `GooglePlayServicesUtil` to do this ... I see your point(API should be consistent) but ... use `GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity)` then `GooglePlayServicesUtil.isUserRecoverableError(resultCode)` and then `GooglePlayServicesUtil.getErrorDialog(resultCode, activity, request).show()` – Selvin Jun 17 '15 at 10:04
  • indeed, that's what I'm going to do.. :/ – ticofab Jun 17 '15 at 10:06
  • Here is a sample that demonstrates using GCM without any other Google Play Services APIs https://github.com/googlesamples/google-services/tree/master/android/gcm – Arthur Thompson Jun 18 '15 at 00:55

2 Answers2

5

It looks like there is no way yet to use GoogleApiClient in conjunction with Google Cloud Messaging. Until then, we need to use the GooglePlayServicesUtil way.

ticofab
  • 7,551
  • 13
  • 49
  • 90
  • 4
    thats a surprise to see wrong instructions in the GCM docs... – Dori Jul 16 '15 at 16:48
  • 6
    GooglePlayServicesUtil is deprecated for this and says to use `GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);` instead – Dori Jul 16 '15 at 17:12
  • Where did you see that @Dori ? I can't even find GoogleApiAvailability class – voghDev Nov 18 '15 at 07:27
  • https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability. Also see http://stackoverflow.com/q/31016722/236743 – Dori Nov 18 '15 at 11:00
0

Try this code

GoogleApiClient apiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(
                                  this /* FragmentActivity */
                                  , this /* OnConnectionFailedListener */)                    
                .addApi(Auth.CREDENTIALS_API)
                .build();
Mohamad.j
  • 79
  • 1
  • 4