问题在Android中获取令牌ID [英] issue in getting token id in android
问题描述
我知道有很多关于这方面的问题,但我无法从这些问题中找出解决方案。
我从GCM的令牌中获得空值。很多人使用类来完成这个,但是我在同一个类的后台线程中做
。它在regId中返回null。
$ b OnCreate
if(checkPlayServices ()){
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if(regid.isEmpty()){
Log.e(TAG,在后台注册);
registerInBackground();
} else {
Log.e(TAG,Notification Token:+ regid);
user.setNotificationToken(regid);
}
} else {
MyLog.i(TAG,找不到有效的Google Play服务APK。);
}
RegisterInBackgroud()if device is not registered before。
private void registerInBackground(){
new AsyncTask< Void,Void,String>(){
@Override
protected String doInBackground(Void ... params){
String msg =;
尝试{
Log.e(标记,在后台执行);
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
if(gcm!= null)
{
Log.e(TAG,GCM is not null);
}
regid = gcm.register(SENDER_ID);
Log.e(TAG,token id:+ regid);
msg =注册的设备,注册ID =+ regid;
storeRegistrationId(context,regid);
} catch(IOException ex){
msg =Error:+ ex.getMessage();
}
return msg;
}
} .execute(null,null,null);
}
我试图记录它,它在regId中显示为空。有什么可能是这个问题?
一些如何我的应用程序ID不起作用。重新在谷歌控制台上的应用程序生成一个新的ID,我在我的项目中使用,它开始工作。
I know that there are lot of questions about this but I couldn't figure out the solution from those questions.
I am getting null in a token from GCM. many people have done this using class, but I am doing this in background thread in same class. It returns null in regId.
OnCreate
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
Log.e(TAG, "registering in background");
registerInBackground();
} else {
Log.e(TAG, "Notification Token : " + regid);
user.setNotificationToken(regid);
}
} else {
MyLog.i(TAG, "No valid Google Play Services APK found.");
}
RegisterInBackgroud() if device is not registered before.
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
Log.e(TAG, "doing in background");
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
if(gcm != null)
{
Log.e(TAG, "GCM is not null");
}
regid = gcm.register(SENDER_ID);
Log.e(TAG, "token id:" + regid);
msg = "Device registered, registration ID=" + regid;
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
}.execute(null, null, null);
}
I tried to log it bug it display null in regId. what could be the problem in this ?
Some how my app id was not working. Recreating app on google console generated a new id which i used in my project and it started working.
这篇关于问题在Android中获取令牌ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!