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

时间:2015-06-17 09:09:55

标签: android google-cloud-messaging google-play-services illegalargumentexception

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

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

尝试运行它会生成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();

2 个答案:

答案 0 :(得分:5)

似乎还没有办法将GoogleApiClient与Google Cloud Messaging结合使用。在此之前,我们需要使用GooglePlayServicesUtil方式。

答案 1 :(得分:0)

尝试此代码

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