Kev*_*vin 206 iphone notifications push apple-push-notifications ios
我正在寻找一种方法来确定用户是否通过设置启用或禁用了我的应用程序的推送通知.
Zac*_*ing 300
打电话enabledRemoteNotificationsTypes检查面具.
例如:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// blah blah blah
Run Code Online (Sandbox Code Playgroud)
iOS8及以上版本:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
Run Code Online (Sandbox Code Playgroud)
Tim*_*ber 99
我无法评论(声誉不够),但重新:quantumpotato的问题:
在哪里types给出
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
Run Code Online (Sandbox Code Playgroud)
一个人可以使用
if (types & UIRemoteNotificationTypeAlert)
Run Code Online (Sandbox Code Playgroud)
代替
if (types == UIRemoteNotificationTypeNone)
Run Code Online (Sandbox Code Playgroud)
将允许您仅检查是否启用了通知(并且不用担心声音,徽章,通知中心等).如果"警报样式"设置为"横幅"或"警报",并且"警报样式"设置为"无",则第一行代码(types & UIRemoteNotificationTypeAlert)将返回YES,与NO其他设置无关.
Kev*_*tre 54
在最新版本的iOS中,此方法现已弃用.要支持iOS 7和iOS 8,请使用:
UIApplication *application = [UIApplication sharedApplication];
BOOL enabled;
// Try to use the newer isRegisteredForRemoteNotifications otherwise use the enabledRemoteNotificationTypes.
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
enabled = [application isRegisteredForRemoteNotifications];
}
else
{
UIRemoteNotificationType types = [application enabledRemoteNotificationTypes];
enabled = types & UIRemoteNotificationTypeAlert;
}
Run Code Online (Sandbox Code Playgroud)
ViJ*_*had 53
更新了swift4.0,iOS11的代码
import UserNotifications
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
//Not authorised
UIApplication.shared.registerForRemoteNotifications()
}
Run Code Online (Sandbox Code Playgroud)
代码为swift3.0,iOS10
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}
Run Code Online (Sandbox Code Playgroud)
从iOS9开始,不推荐使用swift 2.0 UIRemoteNotificationType,请使用以下代码
let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == UIUserNotificationType.none {
// Push notifications are disabled in setting by user.
}else{
// Push notifications are enabled in setting by user.
}
Run Code Online (Sandbox Code Playgroud)
只需检查是否启用了推送通知
if notificationType == UIUserNotificationType.badge {
// the application may badge its icon upon a notification being received
}
if notificationType == UIUserNotificationType.sound {
// the application may play a sound upon a notification being received
}
if notificationType == UIUserNotificationType.alert {
// the application may display an alert upon a notification being received
}
Run Code Online (Sandbox Code Playgroud)
til*_*ilo 33
下面你会找到一个涵盖iOS8和iOS7(及更低版本)的完整示例.请注意,在iOS8之前,您无法区分"禁用远程通知"和"仅启用锁屏启用".
BOOL remoteNotificationsEnabled = false, noneEnabled,alertsEnabled, badgesEnabled, soundsEnabled;
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// iOS8+
remoteNotificationsEnabled = [UIApplication sharedApplication].isRegisteredForRemoteNotifications;
UIUserNotificationSettings *userNotificationSettings = [UIApplication sharedApplication].currentUserNotificationSettings;
noneEnabled = userNotificationSettings.types == UIUserNotificationTypeNone;
alertsEnabled = userNotificationSettings.types & UIUserNotificationTypeAlert;
badgesEnabled = userNotificationSettings.types & UIUserNotificationTypeBadge;
soundsEnabled = userNotificationSettings.types & UIUserNotificationTypeSound;
} else {
// iOS7 and below
UIRemoteNotificationType enabledRemoteNotificationTypes = [UIApplication sharedApplication].enabledRemoteNotificationTypes;
noneEnabled = enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone;
alertsEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeAlert;
badgesEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeBadge;
soundsEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeSound;
}
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"Remote notifications enabled: %@", remoteNotificationsEnabled ? @"YES" : @"NO");
}
NSLog(@"Notification type status:");
NSLog(@" None: %@", noneEnabled ? @"enabled" : @"disabled");
NSLog(@" Alerts: %@", alertsEnabled ? @"enabled" : @"disabled");
NSLog(@" Badges: %@", badgesEnabled ? @"enabled" : @"disabled");
NSLog(@" Sounds: %@", soundsEnabled ? @"enabled" : @"disabled");
Run Code Online (Sandbox Code Playgroud)
Ada*_*aka 25
Swift 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
}
Run Code Online (Sandbox Code Playgroud)
适用于iOS10 +的RxSwift Observable版本:
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*ssy 17
在试图支持iOS8和更低版本时,我没有isRegisteredForRemoteNotifications像Kevin建议的那样运气.相反,我使用过currentUserNotificationSettings,在我的测试中效果很好.
+ (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 {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert) {
isEnabled = YES;
} else{
isEnabled = NO;
}
}
return isEnabled;
}
Run Code Online (Sandbox Code Playgroud)
小智 15
遗憾的是,提供的这些解决方案都没有真正解决问题,因为在提供相关信息时,API在一天结束时严重缺乏.你可以做一些猜测但是使用currentUserNotificationSettings(iOS8 +)只是目前的形式还不足以真正回答这个问题.虽然这里的许多解决方案似乎都暗示了这个或者isRegisteredForRemoteNotifications更确切的答案,但事实并非如此.
考虑一下:
与isRegisteredForRemoteNotifications文档状态:
如果应用程序当前已注册远程通知,则返回YES,同时考虑任何系统范围的设置...
但是,如果你简单地将一个简单地NSLog放入你的app委托来观察行为,很明显这不会像我们预期的那样行事.它实际上直接与此应用/设备激活的远程通知有关.一旦第一次激活,它将始终返回YES.即使在设置(通知)中关闭它们仍然会导致返回YES这是因为,从iOS8开始,应用程序可能会注册远程通知,甚至在没有用户启用通知的情况下发送到设备,他们可能不会执行警报,徽章和声音,用户不打开它.静音通知是即使关闭通知也可以继续执行的一个很好的示例.
只要currentUserNotificationSettings它表明四件事之一:
警报已开启徽章已开启声音已开启无.
这绝对没有任何关于其他因素或通知开关本身的指示.
实际上,用户可以关闭徽章,声音和警报但仍然在锁屏或通知中心显示.此用户仍应接收推送通知,并且能够在锁定屏幕和通知中心中看到它们.他们有通知开关.但是currentUserNotificationSettings将返回:UIUserNotificationTypeNone在那种情况下.这并不是用户实际设置的真实指示.
可以做出一些猜测:
isRegisteredForRemoteNotifications是,NO那么您可以假设此设备从未成功注册过远程通知.application:didRegisterUserNotificationSettings:包含用户通知设置,因为这是用户第一次注册,设置应该指示用户根据权限请求选择的内容.如果设置等同于以下任何内容:UIUserNotificationTypeNone则授予推送权限,否则拒绝.这样做的原因是,从您开始远程注册过程的那一刻起,用户只能接受或拒绝,接受的初始设置是您在注册过程中设置的设置.要完成答案,它可以像这样工作......
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
switch (types) {
case UIRemoteNotificationTypeAlert:
case UIRemoteNotificationTypeBadge:
// For enabled code
break;
case UIRemoteNotificationTypeSound:
case UIRemoteNotificationTypeNone:
default:
// For disabled code
break;
}
Run Code Online (Sandbox Code Playgroud)
编辑:这不对.因为这些是有点用的东西,它不能用开关,所以我结束使用这个:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
UIRemoteNotificationType typesset = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
if((types & typesset) == typesset)
{
CeldaSwitch.chkSwitch.on = true;
}
else
{
CeldaSwitch.chkSwitch.on = false;
}
Run Code Online (Sandbox Code Playgroud)
对于iOS7和之前你应该确实使用enabledRemoteNotificationTypes并检查它是否等于(或不等于你想要的)UIRemoteNotificationTypeNone.
然而,对于iOS8而言,仅仅检查上述状态并不总是足够的isRegisteredForRemoteNotifications.你还应该检查是否application.currentUserNotificationSettings.types等于(或不等于你想要的东西)UIUserNotificationTypeNone!
isRegisteredForRemoteNotifications即使currentUserNotificationSettings.types返回,也可能返回true UIUserNotificationTypeNone.
iOS8+(目标C)
#import <UserNotifications/UserNotifications.h>
[[UNUserNotificationCenter currentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
switch (settings.authorizationStatus) {
case UNAuthorizationStatusNotDetermined:{
break;
}
case UNAuthorizationStatusDenied:{
break;
}
case UNAuthorizationStatusAuthorized:{
break;
}
default:
break;
}
}];
Run Code Online (Sandbox Code Playgroud)