回答
此答案已過時,因此不支持iOS 10,您可以檢查this答案。
使用此代碼
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}
我試圖拉雅的解決方案,但它並沒有給我在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中已被棄用,但仍然有效。
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
對我來說這是截至2017年7月的正確答案。 –
爲什麼不是'如果其他'和'如果其他'? –
@JeremyBader,這是正確的。 –
@拉雅的答案是遠遠不夠的。
isRegisteredForRemoteNotifications
是您的應用程序已連接到APNS並獲得設備令牌,這可能是無聲推送通知currentUserNotificationSettings
是用戶權限,如果沒有這個,沒有發表任何警報,橫幅或聲音推送通知到應用
這裏是支票
static var isPushNotificationEnabled: Bool {
guard let settings = UIApplication.shared.currentUserNotificationSettings
else {
return false
}
return UIApplication.shared.isRegisteredForRemoteNotifications
&& !settings.types.isEmpty
}
如果您的應用支援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")
}
}
...
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)
}
}
}
}
以下是獲取描述了與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
}
- 1. ios10,Swift 3和Firebase推送通知(FCM)
- 2. 在iOS10和iOS9中接收推送通知時遇到問題
- 3. 檢查是否在iOS7上啓用了推送通知
- 4. ios檢查用戶是否禁用推送通知
- 5. 在Swift中推送通知iOS
- 6. iOS是否有遠程推送通知的預算?
- 7. 檢查應用程序是否從推送通知中打開
- 8. 的iOS:處理遠程(推送)通知
- 9. iOS推送通知和遠程通知的區別?
- 10. iOS - 在應用程序中檢查推送通知支持
- 11. Swift - GCM:不顯示iOS遠程推送通知
- 12. 遠程推送通知是否需要在Info.plist中添加UIBackgroundModes?
- 13. 遠程推送通知
- 14. 遠程推送通知
- 15. Swift ios檢查用戶是否啓用了通知總是返回true
- 16. 檢查是否使用Parse發送推送通知 - Android
- 17. 強制啓用推送通知iOS
- 18. 在ios中推送通知
- 19. 應該iOS上的遠程通知觸發推送通知UIApplicationWillResignActiveNotification?
- 20. iOS上的Azure推送通知Swift
- 21. 推送圖像的通知 - iOS - Swift
- 22. Swift iOS 10的推送通知
- 23. Swift IOS 10 Firebase推送通知
- 24. iOS推送通知流程
- 25. IOS應用程序在後臺不顯示推送通知。 Swift
- 26. 檢測是否通過推送通知打開了React Native iOS應用程序
- 27. 我如何知道推送通知是否在iOS中傳遞?
- 28. Xcode6 Swift添加遠程推送通知並從PHP發送
- 29. 如何註冊推送通知? ios10
- 30. Unity3d:Android和iOS推送通知
這似乎不適用於iOS 10.在模擬器中,我單擊「不允許」,並且此代碼仍然表示用戶已註冊用於遠程通知。 – tylerSF
在iOS 10上適用於我。嘗試使用實際設備而不是模擬器。 –
只有當令牌曾經生成(設備已註冊)時纔會告訴您,而不是在通知被阻止的情況下。 – KlimczakM