【问题标题】:what to check for disabled settings for the app in notification center?在通知中心检查应用程序的禁用设置是什么?
【发布时间】:2015-05-26 19:36:22
【问题描述】:
  • 我有如下代码。
  • 我去了通知中心并为我的应用禁用了所有警报、徽章、在锁定屏幕上显示
  • 但下面的代码在我预期为 false 时仍然返回 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; }
    

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    isRegisteredForRemoteNotificaitons 的苹果文档说返回是:

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

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

    如果您想检查UIUserNotificationSettings,那么这应该可以:

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

    编辑:

    如果您使用的是

    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
            }
        }
    }
    

    【讨论】:

    • 这在 iOS 7 中也可以使用吗?...我猜这在 ios 8 中也可以使用
    • 我更新了我的答案以支持 iOS 7。您将在 Xcode 中收到一条警告,指出 UIRemoteNotificationType 在 iOS 8 中已弃用。
    • 只有当 API 在您的部署目标上或之前被弃用时,您才会收到有关已弃用 API 的警告。
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多