8

I'm using the new google cloud messaging

(GoogleCloudMessaging gcm =
           GoogleCloudMessaging.getInstance (context);)

I am following this example, which is very good and works perfectly:

https://github.com/commonsguy/cw-omnibus/tree/master/Push/GCMClient2

With this example I can register in the GCM, but I tried unsuccessfully to unregister.

in the documentation indicates that you should use the following intent:

com.google.android.c2dm.intent.UNREGISTER

And use it as follows:

    

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

not working ...

As I said, the register works fine, but do not know how to unregister.

I have to do more than the intent? What am I doing wrong?

I appreciate any help

Thanks and regards

Eran
  • 387,369
  • 54
  • 702
  • 768
Sergio76
  • 3,835
  • 16
  • 61
  • 88

3 Answers3

16

If you are using the new GoogleCloudMessaging class, you don't need to use the com.google.android.c2dm.intent.UNREGISTER intent. Simply use GoogleCloudMessaging.unregister().

public void unregister ()

Unregister the application. Calling unregister() stops any messages from the server. This is a blocking call—you shouldn't call it from the UI thread. You should rarely (if ever) need to call this method. Not only is it expensive in terms of resources, but it invalidates your registration ID, which should never change unnecessarily. A better approach is to simply have your server stop sending messages. Only use unregister if you want your application to stop using GCM permanently, or you have a compelling reason to recycle your registration ID. Throws IOException if we can't connect to server to unregister.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • can i call this at a time the app is unistalled? if yes how? – Fahim Apr 09 '15 at 06:01
  • @Fahim No, there's no way to run logic when the app is uninstalled. – Eran Apr 09 '15 at 06:14
  • @Fahim You don't need to unregister your device when you app is uninstalled. GCM does it for you itself, as mentioned in this SO answer - http://stackoverflow.com/a/28057615/3913366 . – Shubham A. Oct 27 '15 at 15:26
1

Unregistration may take up to 5 minutes to propagate.

https://developer.android.com/google/gcm/adv.html#unreg-why

  • 3
    While this link may answer the posted question, you better include some excerpts out of it, possibly with some code snippets, since the page might not be available in the future. – Phantômaxx Jun 12 '14 at 10:39
  • Welcome to the future. – Felix Aug 05 '15 at 13:51
0

Old Way (Deprecated):

public synchronized void unregister()

New Way (Instead use):

InstanceID.deleteToken();

or

InstanceID.deleteInstanceID();

Unregister the application. Calling unregister() stops any messages from the server. This is a blocking call—you shouldn't call it from the UI thread. You should rarely (if ever) need to call this method. Not only is it expensive in terms of resources, but it invalidates all your registration IDs returned from register() or subscribe(). This should not be done unnecessarily. A better approach is to simply have your server stop sending messages.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437