2013-04-12 33 views
3

我想在用戶單擊按鈕後取消註冊GCM。我嘗試了以下代碼來取消註冊,但即使在所有這些初始化後,我仍然設法將通知推送到設備。如果我在這裏遺漏任何東西,請告訴我。取消註冊後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 

回答

1

首先,註銷您應使用此代碼:

註銷從GCM

從GCM註銷,做如下:

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。如果沒有被叫,你的設備仍然被註冊。我建議你使用調試器運行應用程序,看看哪些方法被調用。

+0

'unregIntent.putExtra( 「應用程序」,PendingIntent.getBroadcast(此,0,新的意向(),0));'我只需要我分配到SENDER_ID 「應用程序」?我將它更改爲「app」,並刪除了GCMRegistrar.unregister(this)。但我仍然設法收到消息。我在問題中添加了我的日誌。 –

0

放在2行以下注銷您的GCM。

GCMRegistrar.unregister(NotificationsActivity.this); 
GCMRegistrar.onDestroy(NotificationsActivity.this); 
相關問題