【发布时间】:2014-02-21 10:35:51
【问题描述】:
我知道对此有很多问题,但我无法从这些问题中找出解决方案。
我在来自 GCM 的令牌中得到空值。很多人都使用类做到了这一点,但我正在做 这在同一类的后台线程中。它在 regId 中返回 null。
创建时
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() 如果设备之前没有注册过。
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);
}
我试图记录它在 regId 中显示为空的错误。这可能是什么问题?
【问题讨论】:
-
您是否在多个设备上尝试过?
-
是的,我在两台设备上进行了测试。这对他们都不起作用。
-
问题是,当您的设备已经注册到 GCM 时,它将返回 null,否则它将注册您的设备并返回设备 ID。
-
我在 onCreate() 中检查。但那里显示为空,所以我开始注册它。
标签: android-asynctask google-cloud-messaging devicetoken