gpt4 book ai didi

ios - 如果请求通知权限,请选中推送

转载 作者:行者123 更新时间:2023-11-30 11:36:48 28 4
gpt4 key购买 nike

我已经看过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 previous versions 的 else 部分我很困惑,我需要调用

AppDelegate.sharedDelegate.registerForPush()

AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "允许应用发送通知")

?谢谢!

最佳答案

您必须自己存储它,例如在用户默认值中。当您提示用户授予权限时,将标志设置为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
}

关于ios - 如果请求通知权限,请选中推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com