Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM after un-register still receive notification

I wanted to un-register GCM after user clicked on a button. I tried the following codes for un-register but still even after all these initialized I still managed to push notification to the device. Please let me know if I'm missing anything here.

@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();
}

Log

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
like image 222
IssacZH. Avatar asked Jan 30 '26 21:01

IssacZH.


1 Answers

First of all, to un-register you should use this code :

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);

In your code you put SENDER_ID instead of "app".

Second of all, I'm not sure what GCMRegistrar.unregister(this) does (I don't use it), but I think it's a short cut that does the same as the 3 lines of code above, so there's no point in calling both.

Third of all, assuming you are using similar code to what I use for the IntentService (I have a class that extends Google's C2DMBaseReceiver), onUnregistered should be called when the un-registration is complete. If it's not called, your device is still registered. I suggest you run the app with debugger and see which methods are called.

like image 114
Eran Avatar answered Feb 01 '26 10:02

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!