在设置控制器中启用/禁用通知

时间:2014-09-29 12:44:57

标签: ios objective-c cocoa-touch push-notification

我构建的IOS应用使用推送通知。 Apple第一次运行应用程序时,要求您向用户询问他们是否想要这个:

if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

但是,用户可以在使用应用程序时改变主意,并使用UISwitch在设置控制器中打开或关闭通知。

如何捕获当前通知值并从通知中注册/取消注册用户? 这是我在互联网上找到的,但似乎不起作用:

[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

1 个答案:

答案 0 :(得分:2)

如果用户更改了首选项,则无需手动取消注册用户,但您可以随时使用此代码检查状态

if ([[UIApplication sharedApplication]  respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
    if ([[UIApplication sharedApplication]  isRegisteredForRemoteNotifications]){
        NSLog(@"Notifications Enabled iOS 8");
    } else {
        NSLog(@"Notifications Not Enabled iOS 8");
    }
} else {
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert) {
        NSLog(@"Notifications Enabled iOS 7 or older");
    } else {
        NSLog(@"Notifications Not Enabled iOS 7 or older");
    }
}