检查iOS推送通知状态是否未确定。

3

用户登录我的应用后,我会提示用户启用推送通知。我知道如何使用以下方法测试推送通知是否已启用或禁用:

isRegisteredForRemoteNotifications

它可以正常工作。对于已启用的返回YES,对于不可用的返回NO,但我想要能够弄清楚如何检查未确定状态(用户一开始没有提示启用推送通知)。有办法测试吗?

提前致谢!

3个回答

3
你可以自己创建“未确定”状态。
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可能的解决方案,但它们都不起作用,最终我不得不自己实现,至少目前是这样。 - Sikander

2

isRegisteredForRemoteNotifications 是一个 Bool 类型。没有未确定的状态。你可以在文档中查看。

当用户首次安装您的应用程序时,他们必须允许或拒绝推送通知。没有其他可能的选项。

然而,也许你之所以询问是因为你可以删除应用程序,重新安装它,而它不会要求你的许可。那是因为权限已经被记住了。

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

如果你想模拟你的应用程序第一次运行,你可以将应用程序卸载一天。你可以通过以下步骤实现后者,而无需等待一天:

从设备中删除你的应用程序。 完全关闭设备并重新打开。 进入“设置”>“通用”>“日期和时间”,将日期向前调整一天或更多。 再次完全关闭设备并重新打开。

参考文献

相关问题:当我删除我的iOS应用程序时,推送通知状态仍然存在

编辑:

你只能使用 isRegisteredForRemoteNotifications 来检查它们是否未注册,无论是由于拒绝还是因为你从未尝试注册。

但是,只要你以有效的方式尝试注册(有效的证书和配置文件等),并且用户拒绝了,你的应用程序将调用 did register,但是带有空的 UIUserNotificationSettings

 func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    if notificationSettings.types == nil {
        println("You didn't allow notifcations")
    }

}

1
谢谢回复。我问这个问题并不是因为我要删除应用程序。在我的情况下,我是在用户登录后注册远程通知(而不是在启动时),所以有可能通知状态是未确定的。我猜更好的方式来提问是:有没有办法检测用户是否拒绝了推送通知。 - Shlomi Nissan

0
你可以利用 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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接