Registerusernotificationsettings do not work after using unregisterForRemoteNotifications

I want to use something like disabler in my application and activate push notifications. I use this code. I will also activate notifications in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions with code from sender.isOn

 if (sender.isOn) { self.notificationLabel.text = @"Notifications enabled"; UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { self.notificationLabel.text = @"Notifications disabled"; [[UIApplication sharedApplication] unregisterForRemoteNotifications]; } 

But after the first use of unregisterForRemoteNotifications my application does not initialize notifications with registerForRemoteNotifications . I do not understand what I am doing wrong.

+1
source share
1 answer

Solution: Uninstall the application and remove this code!

It took me many hours and 3 broken test devices to track this ...!

This line is Bad :

 [[UIApplication sharedApplication] unregisterForRemoteNotifications] //DON'T DO IT! 

This puts the application in an unstable state when the call to registerForRemoteNotifications no longer works.

This line is equally nasty :

 [[UIApplication sharedApplication] registerForRemoteNotifications] //CALL ME ONCE! 

You must call it once and only once, for launching your application. If you call it twice, notifications will be mysteriously violated.

+2
source

All Articles