草庐IT

ios - isRegisteredForRemoteNotifications 即使注册成功也返回 false

coder 2024-01-28 原文

问题

isRegisteredForRemoteNotifications

即使我已成功收到设备 token ,也返回false

情况

1。注册

在用户安装我的应用程序后,在特定时间点,我会检查 isRegisteredForRemoteNotifications,如果为 false,我会使用上面的代码请求用户允许通知

func registerUserNotificationSettings() {
    let userNotificationTypes: UIUserNotificationType = ([.alert, .badge, .sound])
    let settings = UIUserNotificationSettings(types: userNotificationTypes, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)
  }

并且在显示对话框并且以太用户推送允许或禁止之后,

didRegister notificationSettings

被调用。我们称

application.registerForRemoteNotifications

里面。哪个调用

didRegisterForRemoteNotificationsWithDeviceToken

然后我们将 token 发送到我们的服务器。

2。检查用户是否关闭了推送权限

在上述操作之后,如果使用关闭 iOS 设置应用程序的通知,我会显示一个模态警报,要求用户在某个时候将通知设置更改为 ON。我通过这段代码判断显示模态。

func isApproved() -> Bool {
    guard let setting = UIApplication.shared.currentUserNotificationSettings else {
      return false
    }
    return setting.types.rawValue > 0
  }

如果 isApproved == false,我们显示模态。

3。将用户发送到设置应用

在模态框上,我们放置了一个调用以下方法的按钮

func showAppropriateNotificationConfigView() {

    if UIApplication.shared.isRegisteredForRemoteNotifications == false {
      // case the app has never called registerUserNotificationSettings or registration failed
      registerUserNotificationSettings()

    } else {
      // registerUserNotificationSettings has beeen called and registration succeed
      UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
    }
  }

我们的大多数测试设备返回 true(并且我们确认设备 token 已成功接收)

UIApplication.shared.isRegisteredForRemoteNotifications 

但只有我们的一个设备(iPhone7 加 iOS 11.3)返回 false。 这样

registerUserNotificationSettings

被调用并且不显示任何内容,而不是通过

将用户发送到设置应用程序
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

问题

我的代码有什么问题,我们该如何解决?

我知道有些方法已被弃用,但我需要临时快速修复它。

最佳答案

我通过更新所有相关的弃用方法解决了这个问题。

关于ios - isRegisteredForRemoteNotifications 即使注册成功也返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50114522/

有关ios - isRegisteredForRemoteNotifications 即使注册成功也返回 false的更多相关文章

随机推荐