在通知中心检查应用程序的禁用设置的内容是什么?

时间:2015-05-26 19:36:23

标签: ios objective-c

  • 我的代码如下。
  • 我去了通知中心,并禁用了所有提醒,徽章,锁屏显示我的应用
  • 但是当我预期为假时,下面的代码仍会返回true。
  • 我应该检查通知中心的应用程序的禁用设置是否为假?

    -(BOOL)pushEnabled 
    {
    BOOL enabled = NO;
    
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        enabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    }
    else
    {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        enabled = types & UIRemoteNotificationTypeAlert;
    }
    return enabled; }
    

1 个答案:

答案 0 :(得分:2)

isRegisteredForRemoteNotificaitons的苹果文档说回报是:

如果应用程序已注册远程通知并收到其设备令牌,则为YES;如果未发生注册,失败或用户已拒绝,则为“否”。

因此,如果您成功注册了远程通知,那么无论系统设置如何,isRegisteredForRemoteNotifications都将返回YES

如果您想查看UIUserNotificationSettings,那么这应该有效:

UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
    // not enabled
} else {
    // enabled
}

编辑:

如果您正在使用< iOS 8然后这应该工作:

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

    if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
            // not enabled
        } else {
            // enabled
        }
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types) {
            // enabled
        } else {
            // not enabled
        }
    }
}