将Cloud messaging api添加到GoogleApiClient时出现IllegalArgumentException

tic*_*fab 11 android illegalargumentexception google-cloud-messaging google-play-services

我正在Android应用程序中设置Google Cloud Messaging.一开始,我初始化GoogleApiClient以检查Play服务是否可用:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
Run Code Online (Sandbox Code Playgroud)

试图运行它会产生IllegalArgumentException: must call addApi() to add at least one API,所以我还需要添加GCM Api,但说实话,我在文档中找不到它.就像是:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(gcm.API)     <----- WHAT HERE?
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
Run Code Online (Sandbox Code Playgroud)

tic*_*fab 5

看起来还没有办法GoogleApiClient与Google Cloud Messaging结合使用.在那之前,我们需要使用这种GooglePlayServicesUtil方式.

  • GooglePlayServicesUtil已弃用,并表示要使用`GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);` (6认同)
  • 如果在GCM文档中看到错误的说明,那就太惊讶了...... </ sarcasm> (4认同)