首页 文章

在客户端获取GCM令牌时出错

提问于
浏览
1

包nath.prem.com.premgcmproject;

import android.content.Intent;

import com.google.android.gms.iid.InstanceIDListenerService;

/ ** *由22/7/16的房屋创建 . * / public class GCMTokenRefreshListenerService扩展InstanceIDListenerService {

//如果更改了令牌,则再次注册设备@Override public void onTokenRefresh(){Intent intent = new Intent(this,GCMRegistrationIntentService.class); startService(意向); }}

Error while getting GCM token in client side

致命异常:IntentService []

java.lang.IncompatibleClassChangeError:com.sgoogle.android.gms.iid.zzd.zzdL中的android.support.v4.content.ContextCompat(未知来源)

com.google.android.gms.iid.zzd . (未知来源)com.google.android.gms.iid.zzd . (未知来源)com.google.android.gms.iid.InstanceID.zza(未知来源) )在com.google.android.gms.iid.InstanceID.getInstance(未知来源)

this is the error I am getting while getting gcm token

公共类GCMRegistrationIntentService扩展IntentService {//成功和错误的常量public static final String REGISTRATION_SUCCESS =“RegistrationSuccess”; public static final String REGISTRATION_ERROR =“RegistrationError”; //类构造函数public GCMRegistrationIntentService(){super(“”); } @Override protected void onHandleIntent(Intent intent){//将gcm注册到设备registerGCM(); } private void registerGCM(){//注册完成意图最初为null Intent registrationComplete = null; //注册令牌也为空//我们将在成功注册时获取令牌String token = null; SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try {//创建instanceid InstanceID instanceID = InstanceID.getInstance(this); // // //从实例id获取令牌令牌= instanceID.getToken(getString(R.string.gcm_defaultSenderId),GoogleCloudMessaging.INSTANCE_ID_SCOPE,null); //在日志中显示令牌,以便我们可以将其复制以发送推送通知//您还可以通过将令牌存储到服务器Log.w(“GCMRegIntentService”,“token:”令牌)来扩展应用程序; //注册完成注册成功完成注册完成=新意图(REGISTRATION_SUCCESS); //将令牌放入intent // registrationComplete.putExtra(“token”,token); } catch(Exception e){//如果发生任何错误Log.w(“GCMRegIntentService”,“Registration error”); registrationComplete = new Intent(REGISTRATION_ERROR); } //发送注册完成的广播LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); }}

this is the java code of that class

this is my menifest file in the project......But I cannot see any of the logs in RegistrationIntentService:

1 回答

  • 0

    Try this,

    build.gradle 中添加 compile 'com.google.android.gms:play-services-gcm:9.0.2' 依赖项

    private void registerToGCM() {
            new AsyncTask<String, String, String>() {
                ProgressDialog progressDialog;
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog = new ProgressDialog(LoginActivity.this);
                    progressDialog.setMessage(getString(R.string.gcm_register_message));
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                }
                @Override
                protected String doInBackground(String... params) {
                    String registryId = null;
                    try {
                        InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
                        registryId = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    } catch (Exception ex) {
                        Log.e("MAINACIVITY", "gcm register Error " + ex.toString());
                    }
                    return registryId;
                }
    
                @Override
                protected void onPostExecute(String registeredId) {
                    super.onPostExecute(registeredId);
                    progressDialog.dismiss();
                    // perform action here
                }
            }.execute("");
        }
    

相关问题