2016-07-25 70 views
0

package nath.prem.com.premgcmproject;在客户端获取GCM令牌时出错

import android.content.Intent;

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

/** *由prem于16/17/16创建。 */public class GCMTokenRefreshListenerService extends InstanceIDListenerService {

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

错误而获得GCM令牌在客户端

致命异常:IntentService []

java.lang.IncompatibleClassChangeError:android.support.v4.content.ContextCompat在COM。 google.android.gms.iid.zzd.zzdL(未知来源)

com.google.android.gms.iid.zzd。(未知来源)在com.google.android.gms.iid.zzd。(Unknown Source)com.google.android.gms.iid.InstanceID.zza(Unknown Source)com.google.android.gms.iid.InstanceID.getInstance(Unknown成功和错误公共静态最后弦乐REGISTRATION_SUCCESS =“RegistrationSuccess”源)

这是同时获得GCM令牌

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

这是类

的Java代码

这是在我的项目文件menifest ......但我看不到任何日志在RegistrationIntentService中:

+1

请编辑和格式化你的代码compile 'com.google.android.gms:play-services-gcm:9.0.2'依赖。检查这个http://stackoverflow.com/editing-help – jayeshsolanki93

回答

0

试试这个,

添加在您的build.gradle

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(""); 
    } 
+0

你试过我的解决方案吗? –

相关问题