Swift ios检查是否在ios9和ios10中启用远程推送通知
如何检查用户是否启用了ios 9或ios 10上的远程通知?Swift ios检查是否在ios9和ios10中启用远程推送通知
如果用户未允许或单击否我想切换一条消息询问他们是否要启用通知。
此答案已过时,因此不支持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
}
这似乎不适用于iOS 10.在模拟器中,我单击“不允许”,并且此代码仍然表示用户已注册用于远程通知。 – tylerSF
在iOS 10上适用于我。尝试使用实际设备而不是模拟器。 –
只有当令牌曾经生成(设备已注册)时才会告诉您,而不是在通知被阻止的情况下。 – KlimczakM