3

I'd want my app to only receive remote notifications when the user has signed in. I've been reading Apple's Local and Remote Notification Programming Guide, and I'm not sure if it is required to call registerUserNotificationSettings: when the app launches (application:didFinishLaunchingWithOptions:), or that is just an example and I can register for remote notifications once the user has signed in (if he does).

On the other hand, if I want to stop receiving remote notifications when the user signs out... is it possible to "unregister" the app?

Thanks

AppsDev
  • 12,319
  • 23
  • 93
  • 186

4 Answers4

3

You don't have to register for push/local notifications when the app first launches. It's OK to call it after the user logs in and, in fact, it's probably a wish UI/Flow decision, since it's more likely for the user to agree to receive notifications after already deciding to login to the app.

Regarding logout, you shouldn't "unregister". All you need to do, is send an indication to your server that the user is currently signed out so it shouldn't send any push notifications. The app will still be opt-in for receiving notifications in the future (if the user looks at it on the Settings app on the device, for example).

Rony Rozen
  • 3,957
  • 4
  • 24
  • 46
3

You can register and unregister on login/logout, but this will lead to problems. E.g. if the user reinstalls the app without logging out, you will still have a valid registration without a logged in user. See this question.

You should preventively unregister, when the app is started without a logged in user, to avoid this problem. You should update the registration anyways each time the app is started, so register/unregister at each app start, depending on if a user is logged in or not, additionally to registerting/unregistering at the moment a user logs in/out. This way, if the user reinstalls the app and starts it the first time, it will be unregistered.

This is the best behaviour you can achieve, as Twitter, Facebook, Skype, etc have the same problem: If a logged in user reinstalls the app without logging out first, the device will still receive push notifications for user logged in before deinstallation, even if he is not logged in with the reinstalled app.

Community
  • 1
  • 1
Baris Akar
  • 4,895
  • 1
  • 26
  • 54
2

You don't need to handle it on that level. Just let it register because you have implemented the push service into your app, and then handle push from the server side, when you send the token from your app send also the flag for example "recievePush" and save this entries to your database. This way you will have much more control and flexibility. When the user logs in set the flag to true and when the user logs out set the flag to false. When sending push just check the flag...

AntonijoDev
  • 1,317
  • 14
  • 29
1

@Chonch speaks the truth, BUT if you want to heres how to do it:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    //UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil];
    //[application registerUserNotificationSettings:settings];
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
} else {
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
}

Then you just cancel notifications if your user has logged out or isn't logged in. :)

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73