2016-11-10 101 views

回答

8

此答案已过时,因此不支持iOS 10,您可以检查this答案。


使用此代码

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications 
if isRegisteredForRemoteNotifications { 
    // User is registered for notification 
} else { 
    // Show alert user is not registered for notification 
} 
+0

这似乎不适用于iOS 10.在模拟器中,我单击“不允许”,并且此代码仍然表示用户已注册用于远程通知。 – tylerSF

+0

在iOS 10上适用于我。尝试使用实际设备而不是模拟器。 –

+0

只有当令牌曾经生成(设备已注册)时才会告诉您,而不是在通知被阻止的情况下。 – KlimczakM

26

我试图拉雅的解决方案,但它并没有给我在iOS上10(SWIFT 3)工作。它总是说推送通知已启用。以下是我如何解决问题。如果用户点击了“不允许”或者您还没有询问用户,则表示“未启用”。

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types 
    if notificationType == [] { 
     print("notifications are NOT enabled") 
    } else { 
     print("notifications are enabled") 
    } 

PS:方法currentUserNotificationSettings在iOS 10.0中已被弃用,但仍然有效。

+0

这个工作在iOS 9,8,7等等,还是我需要单独的代码? –

+0

我不确定,我只在iOS 10上检查过它。 – tylerSF

+3

凸轮,我只是在10.2(在手机上)和9.3(在模拟器上)测试了这个代码,它在两者上都有效。 tylerSF,感谢您的解决方案。 – KeithB

31

Apple建议使用UserNotifications框架而不是共享实例。所以,不要忘记导入UserNotifications框架。由于这个框架是iOS的10个新的就真的只有安全使用的应用程序构建此代码为iOS10 +

let current = UNUserNotificationCenter.current() 

current.getNotificationSettings(completionHandler: { (settings) in 
    if settings.authorizationStatus == .notDetermined { 
     // Notification permission has not been asked yet, go for it! 
    } 

    if settings.authorizationStatus == .denied { 
     // Notification permission was previously denied, go to settings & privacy to re-enable 
    } 

    if settings.authorizationStatus == .authorized { 
     // Notification permission was already granted 
    } 
}) 

您可以检查的官方文件以获得更多信息:https://developer.apple.com/documentation/usernotifications

+1

对我来说这是截至2017年7月的正确答案。 –

+1

为什么不是'如果其他'和'如果其他'? –

+0

@JeremyBader,这是正确的。 –

2

@拉雅的答案是远远不够的。

  • isRegisteredForRemoteNotifications是您的应用程序已连接到APNS并获得设备令牌,这可能是无声推送通知
  • currentUserNotificationSettings是用户权限,如果没有这个,没有发表任何警报,横幅或声音推送通知到应用

这里是支票

static var isPushNotificationEnabled: Bool { 
    guard let settings = UIApplication.shared.currentUserNotificationSettings 
    else { 
     return false 
    } 

    return UIApplication.shared.isRegisteredForRemoteNotifications 
    && !settings.types.isEmpty 
} 
10

如果您的应用支援iOS 10和iOS 8,9使用以下代码

//at the top declare UserNotifications 
//to use UNUserNotificationCenter 
import UserNotifications 

然后,

if #available(iOS 10.0, *) { 
     let current = UNUserNotificationCenter.current() 
     current.getNotificationSettings(completionHandler: { (settings) in 
      if settings.authorizationStatus == .notDetermined { 
       // Means you can request 
      } 

      if settings.authorizationStatus == .denied { 
       // User should enable notifications from settings & privacy 
       // show an alert or sth 
      } 

      if settings.authorizationStatus == .authorized { 
       // It's okay, no need to request 
      } 
     }) 
    } else { 
     // Fallback on earlier versions 
     if UIApplication.shared.isRegisteredForRemoteNotifications { 
      print("APNS-YES") 
     } else { 
      print("APNS-NO") 
     } 
    } 
3
在iOS11,斯威夫特4

...

UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
     if settings.authorizationStatus == .authorized { 
      // Already authorized 
     } 
     else { 
      // Either denied or notDetermined 
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { 
       (granted, error) in 
        // add your own 
       UNUserNotificationCenter.current().delegate = self 
       let alertController = UIAlertController(title: "Notification Alert", message: "please enable notifications", preferredStyle: .alert) 
       let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in 
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { 
         return 
        } 
        if UIApplication.shared.canOpenURL(settingsUrl) { 
         UIApplication.shared.open(settingsUrl, completionHandler: { (success) in 
         }) 
        } 
       } 
       let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) 
       alertController.addAction(cancelAction) 
       alertController.addAction(settingsAction) 
       DispatchQueue.main.async { 
        self.window?.rootViewController?.present(alertController, animated: true, completion: nil) 

       } 
      } 
     } 
    } 
2

以下是获取描述了与iOS 9槽iOS的11件作品的当前权限的字符串的解决方案,使用Swift 4.此实现使用When作为承诺。

import UserNotifications 

private static func getNotificationPermissionString() -> Promise<String> { 
    let promise = Promise<String>() 

    if #available(iOS 10.0, *) { 
     let notificationCenter = UNUserNotificationCenter.current() 
     notificationCenter.getNotificationSettings { (settings) in 
      switch settings.authorizationStatus { 
      case .notDetermined: promise.resolve("not_determined") 
      case .denied: promise.resolve("denied") 
      case .authorized: promise.resolve("authorized") 
      } 
     } 
    } else { 
     let status = UIApplication.shared.isRegisteredForRemoteNotifications ? "authorized" : "not_determined" 
     promise.resolve(status) 
    } 

    return promise 
}