I'm having a problem with unregistering notification hub from Azure.
I'm using method unregister()
like this :
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
String connectionString = "xxx";
hub = new NotificationHub("xxx", connectionString, getApplicationContext());
try {
gcm.unregister();
hub.unregister();
Log.d("GCM","Unregister");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
};
I don't get any exception in this code, but I'm still getting push notification. Any help will be apreciated. Thanks in advance.
You shouldn't unregister from GCM.
Why you should rarely unregister
You should only need to unregister in rare cases, such as if you want
an app to stop receiving messages, or if you suspect that the
registration ID has been compromised. In general, once an app has a
registration ID, you shouldn't need to change it.
In particular, you should never unregister your app as a mechanism for
logout or for switching between users, for the following reasons:
A registration ID isn't associated with a particular logged in user.
If you unregister and then re-register, GCM may return the same ID or
a different ID—there's no guarantee either way.
Unregistration may take up to 5 minutes to propagate.
After unregistration, re-registration may again take up to 5 minutes to propagate. During this time messages may be rejected due
to the state of being
unregistered, and after all this, messages may still go to the wrong
user.
More info here: http://developer.android.com/google/gcm/gcm.html#unreg-why
Since you are using Azure Notification Hub, you just need to delete registration from there, not from GCM.
In general, just using hub.unregister()
will suffice. Alternatively, just call hub.register()
with an invalid tag, and it should overwrite the existing registration.
However, I've found that registrations are sometimes duplicated in the Notification Hub (same PNS, different tags/Azure Registration ID), and unregister
only removes one of them, so you keep getting notified. Maybe this happens when you reinstall the app, not sure.
Anyway, to solve this you can delete the registration manually (Visual Studio > Server Explorer > Notification Hubs > Select one > Device registrations tab).
I think you should use a different Thread
to unregister that device. Below way works for me correctly. Please don't forget to keep hub
which is used to register device. In below, I generated a HubKeeper
class and i kept that hub
in HubKeeper
class as static. But differently, I have used FCM instead of GCM
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
if (HubKeeper.hub != null) {
HubKeeper.hub.unregister();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();