IOS - How to disable push notification at logout?
Asked Answered
C

4

10

My app registers the account at login in my server to enable push notification for a chat. Yet I haven't implemented yet the unregistration of the account at logout, so in this moment if I do the login with 2 accounts in the same device it can take the notification of both the accounts. At the same time, my notification center has a POST service which unregisters the 'login_name+ device token' from receive notification center. Where should I call it? Do I have to use unregisterForRemoteNotifications? I just want to unregister the account+Device token from push notification, not to disable the entire app notification forever.

Can I save my device token on didRegisterForRemoteNotificationsWithDeviceToken function like

 $ [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:DEVICE_KEY];

and then, at logout, call my POST function "removeDeviceToken" like

  NSString *deviceToken = [userDefaults objectForKey:DEVICE_KEY];
    if(deviceToken != NULL){
       [self.engine removeDeviceToken:deviceToken];
     }
Clackmannan answered 3/12, 2013 at 8:6 Comment(0)
E
12

I'm not sure if i got it correctly, but if you don't want to disable push notifications for the app, then you should't call the unregisterForRemoteNotifications. What you can do is, when the user taps the logout button, you can make a logout request to your server, which then removes the notificationID from that account, and after the logout request is completed, you just perform the logout locally (update UI etc).

More info about comment:

Yes, first of all, you should call registerForRemoteNotificationTypes method at every launch, because device token can change. After the delegate method

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken

is called, you can get the device token and save it to NSUserDefault. That way when the user logs in, you can get the up-to-date device token (if changed), and send it to your server to be added to that account.

So the code might look like this

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    NSString *newToken = [devToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *notificationID = [newToken description];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:notificationID forKey:@"notification_id"];
    [prefs synchronize];
}

So, now when the user logs in, just get the notification ID and send it to your server

- (IBAction)userTappedLoginButton {
    // Make your login request
    // You can add the notification id as a parameter
    // depending on your web service, or maybe make
    // another request just to update notificationID
    // for a member

    NSString *notificationID = [[NSUserDefaults standardUserDefaults] objectForKey:@"notification_id"];
    ...
    ...
}
Echinoderm answered 3/12, 2013 at 8:15 Comment(7)
yeah, and then at the next login he just call the registerForRemoteNotification as always. Thanks. The problem is only one now: how can i have the device token? have i to save it locally from registerForRemoteNotification call who it is done at every applicationBecomeActive?Clackmannan
Apple docs say, "Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead. Also note that the delegate method may be called any time the device token changes, not just in response to your app registering or re-registering."Milling
URL for above quote: developer.apple.com/library/ios/documentation/…Milling
@AlfieHanssen the link is updated to this linkResponse
"Never cache device tokens; always get them from the system when you need them. Although device tokens are unique to an app and device, they can change over time. The device token can change at any time but is guaranteed to be different when the user restores their device from a backup, when the user installs your app on a new device, and when the user reinstalls the operating system. Fetching the token from the system ensures that you always have the current token needed to communicate with APNS."Response
"In addition, if the token has not changed, fetching it is fast and does not incur any significant overhead.... Important When the device token changes, the user must launch your app once before remote notifications can once again be delivered to that device."Response
I am facing a problem where if the user simply uninstalls the app without logging out, the next user who logs in receives Push Notifications for the previous user. What to do in such cases?Sostenuto
P
24

You can easily enable and disable push notifications in your application by calling

To register, call: registerForRemoteNotificationTypes:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

To unregister, call: unregisterForRemoteNotificationTypes:

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

For check use

Enable or Disable iPhone Push Notifications try this code

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
 // Yes it is..
Postorbital answered 3/12, 2013 at 8:16 Comment(0)
E
12

I'm not sure if i got it correctly, but if you don't want to disable push notifications for the app, then you should't call the unregisterForRemoteNotifications. What you can do is, when the user taps the logout button, you can make a logout request to your server, which then removes the notificationID from that account, and after the logout request is completed, you just perform the logout locally (update UI etc).

More info about comment:

Yes, first of all, you should call registerForRemoteNotificationTypes method at every launch, because device token can change. After the delegate method

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken

is called, you can get the device token and save it to NSUserDefault. That way when the user logs in, you can get the up-to-date device token (if changed), and send it to your server to be added to that account.

So the code might look like this

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    NSString *newToken = [devToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *notificationID = [newToken description];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:notificationID forKey:@"notification_id"];
    [prefs synchronize];
}

So, now when the user logs in, just get the notification ID and send it to your server

- (IBAction)userTappedLoginButton {
    // Make your login request
    // You can add the notification id as a parameter
    // depending on your web service, or maybe make
    // another request just to update notificationID
    // for a member

    NSString *notificationID = [[NSUserDefaults standardUserDefaults] objectForKey:@"notification_id"];
    ...
    ...
}
Echinoderm answered 3/12, 2013 at 8:15 Comment(7)
yeah, and then at the next login he just call the registerForRemoteNotification as always. Thanks. The problem is only one now: how can i have the device token? have i to save it locally from registerForRemoteNotification call who it is done at every applicationBecomeActive?Clackmannan
Apple docs say, "Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead. Also note that the delegate method may be called any time the device token changes, not just in response to your app registering or re-registering."Milling
URL for above quote: developer.apple.com/library/ios/documentation/…Milling
@AlfieHanssen the link is updated to this linkResponse
"Never cache device tokens; always get them from the system when you need them. Although device tokens are unique to an app and device, they can change over time. The device token can change at any time but is guaranteed to be different when the user restores their device from a backup, when the user installs your app on a new device, and when the user reinstalls the operating system. Fetching the token from the system ensures that you always have the current token needed to communicate with APNS."Response
"In addition, if the token has not changed, fetching it is fast and does not incur any significant overhead.... Important When the device token changes, the user must launch your app once before remote notifications can once again be delivered to that device."Response
I am facing a problem where if the user simply uninstalls the app without logging out, the next user who logs in receives Push Notifications for the previous user. What to do in such cases?Sostenuto
C
5

With Swift:

To Register,

UIApplication.shared.registerForRemoteNotifications()

To unregister,

UIApplication.shared.unregisterForRemoteNotifications()
Columbium answered 23/10, 2018 at 11:13 Comment(1)
You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. - BY APPLEVargo
H
3

In general it is a bad idea to unregisterForRemoteNotifications after logout and reregister after login. The reason is simple: if the user logins with another account and you don't specifically check for token overlapping in server, the user will start receiving double notifications.

Hulbig answered 7/4, 2020 at 7:30 Comment(1)
I wanted to comment but yeah, I do not have enough reputation...Hulbig

© 2022 - 2024 — McMap. All rights reserved.