我正在尝试向我的应用程序添加推送通知功能,但我遇到了奇怪的问题。
在我的第一次尝试中,我得到了设备的 regId;但在该设备试图从 GCM 中自行注销之后;实际上它成功了,因为我失去了 regId 但不是我无法再次获得它。
在 LogCat 我看到这些行。
07-12 08:40:25.615: V/GCMRegistrar(10648): Registering receiver
07-12 08:40:25.620: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:25.635: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:28.570: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:28.570: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:29.125: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:29.130: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:39.655: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:39.660: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:40.605: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:40.605: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:41.350: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:41.350: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
07-12 08:40:43.255: D/GCMRegistrar(10648): resetting backoff for PACKAGENAME
07-12 08:40:43.255: V/GCMRegistrar(10648): Unregistering app PACKAGENAME
我试图在我的应用程序中实现 GCM Demo Application 的代码,所以这里是代码。
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// Automatically registers application on
// startup.
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
// Device is already registered on GCM, needs to
// check if it is
// registered on our server as well.
if (!GCMRegistrar.isRegisteredOnServer(this)) {
// Try to register again, but not in the UI
// thread.
// It's also necessary to cancel the thread
// onDestroy(),
// hence the use of AsyncTask instead of a
// raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
mRegisterTask.execute(null, null, null);
}
}
}