使用unregisterForRemoteNotifications后,registerusernotificationsettings不起作用

时间:2016-06-21 16:58:52

标签: ios objective-c push-notification apple-push-notifications

我想在我的应用程序中使用类似推送通知的禁用和启用程序。我使用这样的代码。我还使用- (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初始化通知。我不明白我做错了什么。

1 个答案:

答案 0 :(得分:2)

解决方案:卸载应用并删除该代码!

花了我很多时间和3个破损的测试设备跟踪这个...!

此行错误

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

它会使应用处于不稳定状态,而registerForRemoteNotifications调用不再有效。

此行同样讨厌

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

每次运行应用时,您必须调用一次,并且只能调用一次。如果你碰巧两次打电话,通知会神秘地破坏。

相关问题