Home » Android » android – IllegalArgumentException when adding cloud messaging api to GoogleApiClient

android – IllegalArgumentException when adding cloud messaging api to GoogleApiClient

Posted by: admin May 14, 2020 Leave a comment

Questions:

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();
How to&Answers:

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.

Answer:

Try this code

GoogleApiClient apiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(
                                  this /* FragmentActivity */
                                  , this /* OnConnectionFailedListener */)                    
                .addApi(Auth.CREDENTIALS_API)
                .build();