Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Notifications 检查是否在IOS 8中启用了本地通知_Notifications_Ios8_Localnotification - Fatal编程技术网

Notifications 检查是否在IOS 8中启用了本地通知

Notifications 检查是否在IOS 8中启用了本地通知,notifications,ios8,localnotification,Notifications,Ios8,Localnotification,我在互联网上到处寻找如何使用IOS 8创建本地通知。我找到了许多文章,但没有一篇解释如何确定用户是否设置了“警报”。有人能帮帮我吗!!!我更喜欢使用Objective C而不是Swift。您可以使用UIApplication检查它 希望这有帮助,如果您需要更多信息,请告诉我编辑:查看@simeon's 在Swift中,您需要使用rawValue: let grantedSettings = UIApplication.sharedApplication().currentUserNotifica

我在互联网上到处寻找如何使用IOS 8创建本地通知。我找到了许多文章,但没有一篇解释如何确定用户是否设置了“警报”。有人能帮帮我吗!!!我更喜欢使用Objective C而不是Swift。

您可以使用
UIApplication
检查它


希望这有帮助,如果您需要更多信息,请告诉我编辑:查看@simeon's

在Swift中,您需要使用
rawValue

let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if grantedSettings.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
    // Alert permission granted
} 

要详细介绍Albert的答案,您不需要在Swift中使用
rawValue
。由于
ui用户通知类型
符合
options设置类型
,因此可以执行以下操作:

if let settings = UIApplication.shared.currentUserNotificationSettings {
    if settings.types.contains([.alert, .sound]) {
        //Have alert and sound permissions
    } else if settings.types.contains(.alert) {
        //Have alert permission
    }
}

使用括号
[]
语法组合选项类型(类似于按位或
|
运算符组合其他语言中的选项标志)。

我认为这段代码更精确:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {

    UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];

    if (types & UIUserNotificationTypeBadge) {
        NSLog(@"Badge permission");
    }
    if (types & UIUserNotificationTypeSound){
        NSLog(@"Sound permission");
    }
    if (types & UIUserNotificationTypeAlert){
        NSLog(@"Alert permission");
    }
}

防护装置的Swift

guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
    return
}

以下是Swift 3中的一个简单函数,用于检查是否至少启用了一种类型的通知

享受吧

static func areNotificationsEnabled() -> Bool {
    guard let settings = UIApplication.shared.currentUserNotificationSettings else {
        return false
    }

    return settings.types.intersection([.alert, .badge, .sound]).isEmpty != true
}

感谢MichałKałużny的灵感。

使用@simeon answer Xcode告诉我

iOS 10.0中不推荐使用“currentUserNotificationSettings”:使用UserNotifications框架的-[UnuseNotificationCenter getNotificationSettingsWithCompletionHandler:]和-[UnuseNotificationCenter getNotificationCategoriesWithCompletionHandler:]

下面是使用UNUserNotificationCenter forSwift 4的解决方案:

UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in

        switch settings.alertSetting{
        case .enabled:

            //Permissions are granted

        case .disabled:

            //Permissions are not granted

        case .notSupported:

            //The application does not support this notification type
        }
    }
目标C+iOS 10

  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

        switch (settings.authorizationStatus) {
            case UNAuthorizationStatusNotDetermined:

                break;

            case UNAuthorizationStatusDenied:

                break;

            case UNAuthorizationStatusAuthorized:

                break;

            default:
                break;
        }
    }];

这很有效。如果用户先前拒绝了权限,则类型==无,但再次请求将不会显示请求窗口。我怎么知道他们以前是否否认过?如果我已经问过,我是否应该在用户首选项中保留一个标志?注意,它仅在iOS 8+上可用。注意,这只确定它是否已启用,而不是显式禁用。第二个
if
是否应该类似于if(
grantedSettings.types&(UIUserNotificationTypeSound | UIUserNotificationTypeAlert))
因为7*(111)&2(010)&4(100)之间的位比较将导致0。7是对本杰明所说的——“代码>”(Grand Stutest.Sype和UiSuffNoTrimeType类型No.UIUSENoReTimeCytType警告)永远不会是真的。请考虑添加几行来解释代码。不要使用<代码> RAWValue s,我认为没有任何保证它们永远不会改变。正如@simeon所说,使用
settings.types.contains(.Alert)
noidea@ChrisWagner。我从Matt Neuburg的书上拿的。我不做iOS(一年前我移植了一个Android应用)。我编辑了答案,指向simeon的答案。谢谢你的否决票:)如果你只是想知道用户是否选择了“某物”,那么:
let active=settings.types.intersection([.alert.badge.sound]).isEmpty==false
你的答案是最好的答案Michal Kaluzny+1你能解释一下你的代码是什么吗?如果!=。没有{什么?}还有{什么?}
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

        switch (settings.authorizationStatus) {
            case UNAuthorizationStatusNotDetermined:

                break;

            case UNAuthorizationStatusDenied:

                break;

            case UNAuthorizationStatusAuthorized:

                break;

            default:
                break;
        }
    }];