2014-09-29 40 views
0

我正在構建的IOS應用程序使用推送通知。蘋果要求你詢問用戶它們是否想要這個與否,他們第一次運行該應用程序:在設置控制器中啓用/禁用通知

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

但是,用戶可以使用該應用程序的過程中改變他/她的想法和切換通知或通過使用UISwitch在設置控制器中關閉。

如何捕獲當前通知值並從通知中註冊/取消註冊用戶? 這是我在互聯網上找到,但似乎並沒有工作:

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

看看這個主題:http://stackoverflow.com/questions/1535403/determine-on-iphone-if-user-has-enabled-push-notifications – 2014-09-29 14:00:43

回答

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"); 
    } 
} 
相關問題