registerusernotificationsettings not work after using unregisterForRemoteNotifications

痴心易碎 提交于 2019-12-11 12:57:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!