我想在我的应用程序中使用类似推送通知的禁用和启用程序。我使用这样的代码。我还使用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
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
初始化通知。我不明白我做错了什么。
答案 0 :(得分:2)
解决方案:卸载应用并删除该代码!
花了我很多时间和3个破损的测试设备跟踪这个...!
此行错误:
[[UIApplication sharedApplication] unregisterForRemoteNotifications] //DON'T DO IT!
它会使应用处于不稳定状态,而registerForRemoteNotifications
调用不再有效。
此行同样讨厌:
[[UIApplication sharedApplication] registerForRemoteNotifications] //CALL ME ONCE!
每次运行应用时,您必须调用一次,并且只能调用一次。如果你碰巧两次打电话,通知会神秘地破坏。