【问题标题】:Check Push if notification permission asked or not检查推送是否要求通知权限
【发布时间】:2018-04-05 11:27:04
【问题描述】:

我已经看过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的else部分我很困惑,我需要打电话吗

AppDelegate.sharedDelegate.registerForPush()

AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")

?谢谢!

【问题讨论】:

    标签: ios swift push-notification


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2014-10-05
      相关资源
      最近更新 更多