Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查是否未确定iOS推送通知状态_Ios_Objective C - Fatal编程技术网

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

检查是否未确定iOS推送通知状态,ios,objective-c,Ios,Objective C,在用户登录到我的应用程序后,我会提示用户启用推送通知。我知道如何使用以下方法测试推送通知是否启用或禁用: isRegisteredForRemoteNotifications 而且效果很好。对于已启用,它返回YES,对于不可用,它返回NO,但我希望能够找出如何检查未确定(没有提示用户首先启用推送通知)。有没有办法测试一下 提前谢谢 isRegisteredForRemoteNotifications是一个Bool。没有未确定的状态。您可以验证这是正确的 当用户首次安装您的应用程序时,他们必须允

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

isRegisteredForRemoteNotifications
而且效果很好。对于已启用,它返回YES,对于不可用,它返回NO,但我希望能够找出如何检查未确定(没有提示用户首先启用推送通知)。有没有办法测试一下


提前谢谢

isRegisteredForRemoteNotifications
是一个
Bool
。没有未确定的状态。您可以验证这是正确的

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

然而,也许你这样问是因为你可以删除应用程序,重新安装,而它不会要求你的许可。那是因为许可被记住了

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

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

从设备中删除你的应用程序。 完全关闭设备并重新打开。 进入设置>常规>日期和时间,将日期提前一天或更长时间。 再次完全关闭设备并重新打开

相关问题:

编辑:

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

但是,只要您尝试以有效的方式(有效的证书和配置文件等)注册,而用户拒绝,您的应用程序将调用did register,但使用nil
UIUserNotificationSettings

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

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

}

您创建
未确定
说明您自己

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)
}
didfishlaunchingwithoptions
方法中,您需要检查是否调用了
registerNotification()


最后,您可以根据需要随时随地直接调用
registerNotification()
,警报现在由您控制。

您可以使用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")
            }
        }
    }
在DidFinishLaunching中使用它,选项如下:

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

谢谢你的回复。我没有问,因为我正在尝试删除该应用程序。在我的例子中,我在用户登录后(而不是在启动时)注册了远程通知,因此通知状态将不确定。我想问这个问题的一个更好的方法是:是否有一个方法来检测用户是否拒绝推送通知。我尝试了iOS上所有可能的解决方案,但都不起作用,最后我必须自己实现,至少现在是这样。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.registerForRemoteNotifications()
    self.checkNotificationsAuthorizationStatus()
    return true
}