我已经看过Determine on iPhone if user has enabled push notifications
但我的问题有些不同。我需要显示一个"导航到设置"屏幕,如果用户拒绝推送通知权限。我们如何检查之前是否提出了推送权限? if #available(iOS 10.0, *) {
工作正常,可以获取所有status
,但对于早期版本,我无法弄明白。
我有以下代码:
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: .main) {[weak self] (notificaiont) in
guard let strongSelf = self else {return }
if #available(iOS 10.0, *) {
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: {settings in
switch settings.authorizationStatus {
case .notDetermined:
AppDelegate.sharedDelegate.registerForPush()
case .denied:
AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")
case .authorized:
strongSelf.dismiss(animated: true, completion: nil)
break
}
})
} else {
// Fallback on earlier versions
if UIApplication.shared.isRegisteredForRemoteNotifications {
strongSelf.dismiss(animated: true, completion: nil)
} else {
//what should i call here registerForPush for navigate to setting ?
// AppDelegate.sharedDelegate.registerForPush()
AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")
}
}
}
在// Fallback on earlier versions
其他部分我感到困惑,我是否需要致电
AppDelegate.sharedDelegate.registerForPush()
或
AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")
?谢谢!
答案 0 :(得分:0)
您必须自己存储,例如在用户默认值。当您提示用户获得权限时,请将标志设置为true
;像这样的东西:
if UIApplication.shared.isRegisteredForRemoteNotifications {
// equivalent to .authorized case
}
else if !UserDefaults.standard.bool(forKey: "didAskForPushPermission")
{
// equivalent to .notDetermined case
UserDefaults.standard.set(true, forKey: "didAskForPushPermission")
}
else {
// equivalent to .denied case
}