如何检查iPhone上是否启用了通知

时间:2012-01-25 16:06:15

标签: iphone xcode push-notification apple-push-notifications

如何检查iPhone上是否启用了通知? 我安装应用程序,应用程序说确认启用应用程序的推送通知,我单击确定。但是,如果在iPhone上禁用通知,则此操作不会启用通知。 怎么检查?

2 个答案:

答案 0 :(得分:8)

这应该有效:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   // Disabled

答案 1 :(得分:3)

这在iOS8中有所改变。要同时支持iOS8和更低版本

+ (BOOL)notificationServicesEnabled {
    BOOL isEnabled = NO;

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
            isEnabled = NO;
        } else {
            isEnabled = YES;
        }
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert) {
            isEnabled = YES;
        } else{
            isEnabled = NO;
        }
    }

    return isEnabled;
}