【问题标题】:Check if iOS push notifications status is not determined检查是否未确定 iOS 推送通知状态
【发布时间】:2015-07-17 06:52:52
【问题描述】:

我正在提示我的用户在用户登录到我的应用程序后启用推送通知。我知道如何测试是否启用或禁用推送通知:

isRegisteredForRemoteNotifications

它工作得很好。它返回 YES 表示已启用,返回 NO 表示不可用,但我希望能够弄清楚如何检查未确定(未提示用户开始启用推送通知)。有没有办法测试?

提前致谢!

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    isRegisteredForRemoteNotificationsBool。没有未确定的状态。您可以验证这是reference

    当用户首次安装您的应用时,他们必须允许或禁止推送通知。没有其他可能的选择。

    但是,您可能会问,因为您可以删除应用程序,重新安装,它不会征求您的许可。那是因为权限被记住了。

    在 iOS 上重置推送通知权限警报 启用推送的应用第一次注册推送通知时,iOS 会询问用户是否希望接收该应用的通知。一旦用户对此警报做出响应,除非设备已恢复或应用已卸载至少一天,否则不会再次显示。

    如果您想模拟应用的首次运行,可以将应用卸载一天。您可以按照以下步骤实现后者,而无需实际等待一天:

    从设备中删除您的应用。 完全关闭设备并重新打开。 前往“设置”>“通用”>“日期和时间”,将日期提前一天或更长时间。 再次完全关闭设备,然后重新打开。

    Reference

    相关问题:When I delete my iOS application push notification state remains

    编辑:

    您只能使用isRegisteredForRemoteNotifications 来检查他们是否根本没有注册,无论是因为拒绝还是因为您从未尝试注册。

    但是,只要您尝试以有效的方式(有效的证书和配置文件等)注册并且用户拒绝,您的应用就会调用 did register,但没有UIUserNotificationSettings

     func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    
        if notificationSettings.types == nil {
            println("You didn't allow notifcations")
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。我没有问,因为我正在尝试删除该应用程序。就我而言,我在用户登录后(不是在启动时)注册了远程通知,因此存在通知状态未确定的情况。我想问这个问题的更好方法是:是否有一个检测用户是否拒绝推送通知。
    【解决方案2】:

    您自己创建Not Determined 状态。

    func registerNotification() {
        // 1.Call register API
        // ...
    
        // 2.Save a bool value to indicate you've called this method or not.
        let appleSuggestedUserDefaultsKeyPrefix = "com.yourcompany.product-"
        let key = appleSuggestedUserDefaultsKeyPrefix + "didCallRegisterNotificationAPI"
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: key)
    }
    

    didFinishLaunchingWithOptions 方法中,你需要检查你是否调用了registerNotification()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        let didCallRegisterNotificationAPI = NSUserDefaults.standardUserDefaults().boolForKey(...)
        if didCallRegisterNotificationAPI {
            // If registered or user denied, call this method will NOT show the alert. 
            // Just the same as you did before.
            registerNotification()
        } else {
            print("registerNotification() has not been called")
        }
    }
    

    最后,您可以根据需要随时随地直接拨打registerNotification(),警报现在就在您的控制之下。

    【讨论】:

    • 我尝试了所有可能的 iOS 解决方案,但没有一个有效,最后我必须自己实现,至少现在。
    【解决方案3】:

    您可以使用 UNNotificationSettings 的 authorizationStatus 属性。

    private func checkNotificationsAuthorizationStatus() {
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.getNotificationSettings { notificationSettings in
            switch notificationSettings.authorizationStatus {
            case .authorized:
                print("Authorized to schedule or receive notifications.")
            case .denied:
                print("Not authorized to schedule or receive notifications.")
            case .notDetermined:
                print("Not determined whether the app is allowed to schedule notifications.")
            case .provisional: // Available from iOS 12.0
                print("Provisionally authorized to post noninterruptive user notifications.")
            @unknown default:
                print("Error")
                }
            }
        }
    

    在 didFinishLaunchingWithOptions 中使用它,例如:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.registerForRemoteNotifications()
        self.checkNotificationsAuthorizationStatus()
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多