使用 unregisterForRemoteNotifications 后 registerusernotificationsettings 不起作用 [英] registerusernotificationsettings not work after using unregisterForRemoteNotifications
问题描述
我想在我的应用程序中使用诸如推送通知的禁用程序和启用程序之类的东西.我使用这样的代码.另外我在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中激活通知,代码来自 sender.isOn
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];
}
但是在第一次使用 unregisterForRemoteNotifications
之后,我的应用程序没有使用 registerForRemoteNotifications
初始化通知.我不明白我做错了什么.
But after first use unregisterForRemoteNotifications
my app not initialling notifications with registerForRemoteNotifications
. I don't understand what I'm doing wrong.
推荐答案
解决方案:卸载应用并删除该代码!
我花了好几个小时和 3 个坏掉的测试设备来追踪这个..!
It took me many hours and 3 broken test devices to track this one down..!
这条线不好:
[[UIApplication sharedApplication] unregisterForRemoteNotifications] //DON'T DO IT!
它使应用程序处于不稳定状态,调用 registerForRemoteNotifications
不再有效.
It puts the app in an unstable state where calling registerForRemoteNotifications
no longer works.
这条线同样讨厌:
[[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.
这篇关于使用 unregisterForRemoteNotifications 后 registerusernotificationsettings 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!