【发布时间】:2014-11-21 00:22:33
【问题描述】:
我在整个互联网上查看了如何使用 IOS 8 创建本地通知。我找到了很多文章,但没有一篇文章解释了如何确定用户是否已将“警报”设置为打开或关闭。有人可以帮我吗!!!我更喜欢使用 Objective C 而不是 Swift。
【问题讨论】:
标签: notifications ios8 localnotification
我在整个互联网上查看了如何使用 IOS 8 创建本地通知。我找到了很多文章,但没有一篇文章解释了如何确定用户是否已将“警报”设置为打开或关闭。有人可以帮我吗!!!我更喜欢使用 Objective C 而不是 Swift。
【问题讨论】:
标签: notifications ios8 localnotification
您可以使用UIApplication的currentUserNotificationSettings查看它
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
}
希望对您有所帮助,如果您需要更多信息,请告诉我
【讨论】:
if 不应该类似于 if (grantedSettings.types & (UIUserNotificationTypeSound | UIUserNotificationTypeAlert)),因为 7* (111) & 2 (010) & 4 (100) 之间的按位比较将导致 0. 7是所有权限
(grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert) 永远不会是真的
要扩展 Albert 的答案,您不需要在 Swift 中使用 rawValue。因为UIUserNotificationType 符合OptionSetType,所以可以执行以下操作:
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
}
}
您使用方括号 [] 语法来组合选项类型(类似于按位或 | 运算符用于组合其他语言中的选项标志)。
【讨论】:
let active = settings.types.intersection([.alert, .badge,.sound]).isEmpty == false
Swift 与 guard:
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。
在 Swift 中,你需要使用rawValue:
let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if grantedSettings.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
// Alert permission granted
}
【讨论】:
rawValues,我认为不能保证它们永远不会改变。正如@simeon 所说,使用settings.types.contains(.Alert)
我认为这段代码更精确:
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");
}
}
【讨论】:
使用@simeon 回答 Xcode 告诉我
'currentUserNotificationSettings' 在 iOS 10.0 中已弃用:使用 UserNotifications Framework 的 -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] 和 -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]
所以这是使用 Swift 4 的 UNUserNotificationCenter 的解决方案:
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;
}
}];
【讨论】: