我正在寻找一种方法来确定用户是否有,通过设置,启用或禁用他们的推送通知我的应用程序。
当前回答
不幸的是,所提供的这些解决方案都没有真正解决问题,因为在提供相关信息时,api严重缺乏。你可以做一些猜测,但是使用currentUserNotificationSettings (iOS8+)在目前的形式下还不足以真正回答这个问题。虽然这里的许多解决方案似乎表明,要么是that,要么是isregisteredforremotenotification是一个更明确的答案,但实际上不是。
考虑一下:
with isregisteredforremotenotifations文档声明:
如果应用程序当前为远程通知注册,则返回YES,考虑到任何系统范围的设置…
However if you throw a simply NSLog into your app delegate to observe the behavior it is clear this does not behave the way we are anticipating it will work. It actually pertains directly to remote notifications having been activated for this app/device. Once activated for the first time this will always return YES. Even turning them off in settings (notifications) will still result in this returning YES this is because, as of iOS8, an app may register for remote notifications and even send to a device without the user having notifications enabled, they just may not do Alerts, Badges and Sound without the user turning that on. Silent notifications are a good example of something you may continue to do even with notifications turned off.
就currentUserNotificationSettings而言,它表示以下四件事之一:
警报开启 徽章都戴上了 声音开着 没有打开。
这完全没有给你任何关于其他因素或通知开关本身的指示。
事实上,用户可能会关闭徽章、声音和提醒,但仍然会在锁定屏幕或通知中心显示。该用户仍然可以收到推送通知,并且能够在锁定屏幕和通知中心看到它们。他们打开了通知开关。但是currentUserNotificationSettings会返回:UIUserNotificationTypeNone。这并不能真正反映用户的实际设置。
我们可以做一些猜测:
if isRegisteredForRemoteNotifications is NO then you can assume that this device has never successfully registered for remote notifications. after the first time of registering for remote notifications a callback to application:didRegisterUserNotificationSettings: is made containing user notification settings at this time since this is the first time a user has been registered the settings should indicate what the user selected in terms of the permission request. If the settings equate to anything other than: UIUserNotificationTypeNone then push permission was granted, otherwise it was declined. The reason for this is that from the moment you begin the remote registration process the user only has the ability to accept or decline, with the initial settings of an acceptance being the settings you setup during the registration process.
其他回答
斯威夫特 3+
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
// settings.authorizationStatus == .authorized
})
} else {
return UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false
}
RxSwift可观察的iOS10+版本:
import UserNotifications
extension UNUserNotificationCenter {
static var isAuthorized: Observable<Bool> {
return Observable.create { observer in
DispatchQueue.main.async {
current().getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
if settings.authorizationStatus == .authorized {
observer.onNext(true)
observer.onCompleted()
} else {
current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in
observer.onNext(granted)
observer.onCompleted()
}
}
})
}
return Disposables.create()
}
}
}
我尝试使用@Shaheen ghassy提供的解决方案支持iOS 10及以上版本,但发现剥夺问题enabledRemoteNotificationTypes。所以,我找到的解决方案是使用isregisteredforremotenotification而不是enabledRemoteNotificationTypes,这在iOS 8中已弃用。下面是我最新的解决方案,对我来说非常有效:
- (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 {
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
isEnabled = YES;
} else{
isEnabled = NO;
}
}
return isEnabled;
}
我们可以很容易地调用这个函数,访问它的Bool值,并将其转换为字符串值:
NSString *str = [self notificationServicesEnabled] ? @"YES" : @"NO";
希望它也能帮助到其他人:) 快乐的编码。
re:
这是正确的
if (types & UIRemoteNotificationTypeAlert)
但是下面也是正确的!(因为UIRemoteNotificationTypeNone是0)
if (types == UIRemoteNotificationTypeNone)
参见以下内容
NSLog(@"log:%d",0 & 0); ///false
NSLog(@"log:%d",1 & 1); ///true
NSLog(@"log:%d",1<<1 & 1<<1); ///true
NSLog(@"log:%d",1<<2 & 1<<2); ///true
NSLog(@"log:%d",(0 & 0) && YES); ///false
NSLog(@"log:%d",(1 & 1) && YES); ///true
NSLog(@"log:%d",(1<<1 & 1<<1) && YES); ///true
NSLog(@"log:%d",(1<<2 & 1<<2) && YES); ///true
下面是如何在Xamarin.ios中做到这一点。
public class NotificationUtils
{
public static bool AreNotificationsEnabled ()
{
var settings = UIApplication.SharedApplication.CurrentUserNotificationSettings;
var types = settings.Types;
return types != UIUserNotificationType.None;
}
}
如果你支持iOS 10+,只使用UNUserNotificationCenter方法。
不幸的是,所提供的这些解决方案都没有真正解决问题,因为在提供相关信息时,api严重缺乏。你可以做一些猜测,但是使用currentUserNotificationSettings (iOS8+)在目前的形式下还不足以真正回答这个问题。虽然这里的许多解决方案似乎表明,要么是that,要么是isregisteredforremotenotification是一个更明确的答案,但实际上不是。
考虑一下:
with isregisteredforremotenotifations文档声明:
如果应用程序当前为远程通知注册,则返回YES,考虑到任何系统范围的设置…
However if you throw a simply NSLog into your app delegate to observe the behavior it is clear this does not behave the way we are anticipating it will work. It actually pertains directly to remote notifications having been activated for this app/device. Once activated for the first time this will always return YES. Even turning them off in settings (notifications) will still result in this returning YES this is because, as of iOS8, an app may register for remote notifications and even send to a device without the user having notifications enabled, they just may not do Alerts, Badges and Sound without the user turning that on. Silent notifications are a good example of something you may continue to do even with notifications turned off.
就currentUserNotificationSettings而言,它表示以下四件事之一:
警报开启 徽章都戴上了 声音开着 没有打开。
这完全没有给你任何关于其他因素或通知开关本身的指示。
事实上,用户可能会关闭徽章、声音和提醒,但仍然会在锁定屏幕或通知中心显示。该用户仍然可以收到推送通知,并且能够在锁定屏幕和通知中心看到它们。他们打开了通知开关。但是currentUserNotificationSettings会返回:UIUserNotificationTypeNone。这并不能真正反映用户的实际设置。
我们可以做一些猜测:
if isRegisteredForRemoteNotifications is NO then you can assume that this device has never successfully registered for remote notifications. after the first time of registering for remote notifications a callback to application:didRegisterUserNotificationSettings: is made containing user notification settings at this time since this is the first time a user has been registered the settings should indicate what the user selected in terms of the permission request. If the settings equate to anything other than: UIUserNotificationTypeNone then push permission was granted, otherwise it was declined. The reason for this is that from the moment you begin the remote registration process the user only has the ability to accept or decline, with the initial settings of an acceptance being the settings you setup during the registration process.
推荐文章
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销