One of the feature in my app is to unregister user from the remote pushnotification when user logs-out from the app. Is there way I can unregister the app from push notification when user logs out while the device is in airplane(offline) mode?
I tried this code but I still receive notifications when I come back online, [[UIApplication sharedApplication] unregisterForRemoteNotifications];
Could some one help how to remove app from notification center when the device is in airplane mode?
Apple controls remote push notifications. If you are offline you logically cannot disable it. When Apple first sees you online and pushes are not disabled, it will send you pushes. There is no way to stop the iDevice from showing the pushes that apple sends to the user, as it is not in the hands of the app.
So there is only one possibility: You have to disable the pushes IMMEDIATELY after the device went online again. And still it is a question of luck, what message Apple obtains first, that you are online or that you disable push. And also there is the question, what will happen to the notifications that Apple wanted to send to the device while it was offline. Will it resend them anyway when online or discard them when disabling first? I don't know. You'll need to do the research.
So the only chance you have is if your app is in the background while the user goes online. You'll need to prevent your app from getting suspended (here's how to achieve that) in order to be able to respond to system notifications. The notification that you need is that of change of online status, I only know it exists. This resource may be helpful. When the device goes online it therefore will notify your app which waits in the background (not suspended). Then you can react by unregistering frm remote pushes immediately.
That's the best you possibly could achieve in that direction. Hope this helps.
Add a flag or something similar (call it whatever you want, like ShouldRecallUnregister), when you call
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
that you set to YES if there is no internet connexion, and leave it at NO if the unregistering works.
Store it in the UserDefaults (
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ShouldRecallUnregister"];
), and check its value inapplicationDidBecomeActive:
for example (or wherever it suits best your app).If it's YES, call
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
again.