注销 GCM 后仍然接收到通知

3

我希望在用户点击一个按钮后取消GCM的注册。我尝试了以下代码来取消注册,但即使我初始化了所有这些内容,我仍然可以向设备推送通知。如果我漏掉了什么,请让我知道。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.options_logout:
            ServerUtilities.unregister(getApplicationContext(), ServerUtilities.registrationID);
            unRegisterGCM();
            return true;
        case R.id.options_exit:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onDestroy() {
    Log.i(TAG,"onDestroy.........");
    helper.close();
    if (mRegisterTask != null) {
        mRegisterTask.cancel(true);
    }
    unregisterReceiver(mHandleMessageReceiver);
    super.onDestroy();
}

public void unRegisterGCM(){
    Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
    unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    startService(unregIntent);

    Intent intent = new Intent(MessageList.this, LoginScreen.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
}

日志

04-12 16:20:23.039: I/MessageList(1912): onDestroy.........
04-12 16:20:26.523: V/GCMBroadcastReceiver(1912): onReceive: com.google.android.c2dm.intent.REGISTRATION
04-12 16:20:26.523: V/GCMBroadcastReceiver(1912): GCM IntentService class: com.mon.GCMIntentService
04-12 16:20:26.523: V/GCMBaseIntentService(1912): Acquiring wakelock
04-12 16:20:26.531: V/GCMBaseIntentService(1912): Intent service name: GCMIntentService-0987654321-2
04-12 16:20:26.531: E/GCMRegistrar(1912): internal error: retry receiver class not set yet
04-12 16:20:26.531: V/GCMRegistrar(1912): Registering receiver
04-12 16:20:26.535: D/GCMBaseIntentService(1912): handleRegistration: registrationId = null, error = null, unregistered = com.mon
04-12 16:20:26.535: D/GCMRegistrar(1912): resetting backoff for com.fl.monitor
04-12 16:20:26.535: V/GCMRegistrar(1912): Saving regId on app version 1
04-12 16:20:26.621: I/GCMIntentService(1912): Device unregistered
04-12 16:20:26.621: V/GCMRegistrar(1912): Is registered on server: false
04-12 16:20:26.621: I/GCMIntentService(1912): Ignoring unregister callback
04-12 16:20:26.621: V/GCMBaseIntentService(1912): Releasing wakelock
04-12 16:20:58.156: V/GCMBroadcastReceiver(1912): onReceive: com.google.android.c2dm.intent.RECEIVE
04-12 16:20:58.156: V/GCMBroadcastReceiver(1912): GCM IntentService class: com.mon.GCMIntentService
04-12 16:20:58.156: V/GCMBaseIntentService(1912): Acquiring wakelock
04-12 16:20:58.160: V/GCMBaseIntentService(1912): Intent service name: GCMIntentService-1234567890-3
04-12 16:20:58.164: I/GCMIntentService(1912): Received message
04-12 16:20:58.167: I/GCMIntentService(1912): project:GHI
2个回答

1

首先,要取消注册,您应该使用此代码:

Unregistering from GCM

To unregister from GCM, do the following:

Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);

在您的代码中,您将SENDER_ID放入了"app"。

第二,我不确定GCMRegistrar.unregister(this)是什么意思(我没有使用它),但我认为它是一个快捷方式,与上面3行代码执行相同的操作,因此无需同时调用两者。

第三,假设您正在使用类似于我使用的IntentService的代码(我有一个扩展Google的C2DMBaseReceiver的类),当注销完成时应该调用onUnregistered。如果未调用,则您的设备仍处于注册状态。建议您使用调试器运行应用程序,并查看调用了哪些方法。


unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 我只需要把我的SENDER_ID分配给“app”吗?我将其更改为“app”,并删除了GCMRegistrar.unregister(this)。但我仍然成功接收到消息。我将我的Log添加到问题中。 - IssacZH.

0

在下面放置2行代码以注销您的GCM。

GCMRegistrar.unregister(NotificationsActivity.this);
GCMRegistrar.onDestroy(NotificationsActivity.this);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接