2016-11-10 59 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 
}