问题
I want to use in my app something like disabler and enabler of push notifications. I use code like this. Also I 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 first use unregisterForRemoteNotifications my app not initialling notifications with registerForRemoteNotifications. I don't understand what I'm doing wrong.
回答1:
Solution: Uninstall the app and delete that code!
It took me many hours and 3 broken test devices to track this one down..!
This line is bad:
[[UIApplication sharedApplication] unregisterForRemoteNotifications] //DON'T DO IT!
It puts the app in an unstable state where calling registerForRemoteNotifications no longer works.
This line is equally nasty:
[[UIApplication sharedApplication] registerForRemoteNotifications] //CALL ME ONCE!
You must call it once, and only once, per run of your app. If you happen to call it twice, notifications will mysteriously break.
来源:https://stackoverflow.com/questions/37950399/registerusernotificationsettings-not-work-after-using-unregisterforremotenotific