package nath.prem.com.premgcmproject;
导入android.content.Intent;
导入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(意向); }}
在客户端获取GCM令牌时出错
致命异常:IntentService []
com.google.android.gms.iid.zzd.zzdL(未知来源)中的java.lang.IncompatibleClassChangeError:android.support.v4.content.ContextCompat
在
来自com.google.android.gms.iid.Instance.zza的com.google.android.gms.iid.zzd。(未知来源)的com.google.android.gms.iid.zzd。(未知来源) (未知来源)com.google.android.gms.iid.InstanceID.getInstance(未知来源)这是我在获取gcm令牌时遇到的错误
公共类GCMRegistrationIntentService扩展IntentService {//成功和错误的常量public static final String REGISTRATION_SUCCESS =" RegistrationSuccess&#34 ;; public static final String REGISTRATION_ERROR =" RegistrationError&#34 ;; //类构造函数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",&#34 ; token:" + token); //在注册时完成注册成功注册完成注册完成=新意图(REGISTRATION_SUCCESS); //将令牌放入intent // registrationComplete.putExtra(" token",token); } catch(例外e){//如果发生任何错误Log.w(" GCMRegIntentService","注册错误"); registrationComplete = new Intent(REGISTRATION_ERROR); } //发送注册完成的广播LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); }}
这是该类的java代码
这是我在项目中的清单文件......但我在RegistrationIntentService中看不到任何日志:
答案 0 :(得分:0)
试试这个,
在compile 'com.google.android.gms:play-services-gcm:9.0.2'
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("");
}