IOS How to disable push notification when logging out?

my application registers an account during authorization on my server in order to enable push notification for chat, but I have not yet unregistered my account when I log out, so at this point if I login with two accounts to the same device takes notice of both accounts. At the same time, my notification center has a POST service, which unregisters the login_name + device token from the notification center. Where should I call it? Do I use unregisterForRemoteNotifications? I just want to unregister accout + Device from the push notification, and not permanently disable the application notification.

Can I save the device token in the didRegisterForRemoteNotificationsWithDeviceToken function, for example

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

and then, when logging out, call my POST function "removeDeviceToken", for example

  NSString *deviceToken = [userDefaults objectForKey:DEVICE_KEY]; if(deviceToken != NULL){ [self.engine removeDeviceToken:deviceToken]; } 
+7
ios apple-push-notifications
source share
2 answers

I am not sure if I received it correctly, but if you do not want to disable push notifications for the application, you should not call unregisterForRemoteNotifications. What can you do when a user deletes the logout button, you can make a request to log out to your server, which then removes the notification ID from this account, and after completing the logout request, you simply do a local logout (update the interface, etc. d.).

Read more about comments:

Yes, first of all, you must call the registerForRemoteNotificationTypes method every time you start, because the device token can change. After the delegate method

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

You can get the device token and save it in NSUserDefault. Thus, when the user logs in, you can get the updated device token (if changed) and send it to your server to add to this 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 that the user is logged 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"]; ... ... } 
+4
source share

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

To register calls registerForRemoteNotificationTypes

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

For unregiste call unregisterForRemoteNotificationTypes

 [[UIApplication sharedApplication] unregisterForRemoteNotifications]; 

To check use

Enable or disable Iphone Push notifications with this code

 UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types == UIRemoteNotificationTypeNone) // Yes it is.. 
+21
source share

All Articles