ios - UIApplication.registerForRemoteNotifications() 只能从主线程调用

标签 ios swift push-notification swift4 unusernotificationcenter

Xcode 9 (iOS 11) 在注册推送(远程)通知时向我显示错误/警告。

这里是错误信息

enter image description here

这是我试过的代码:

let center  = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
        if error == nil{
              UIApplication.shared.registerForRemoteNotifications()
        }
 }

错误/警告线:

UIApplication.shared.registerForRemoteNotifications()

如何解决?

最佳答案

swift4

你可以解决这个问题

DispatchQueue.main.async {
  UIApplication.shared.registerForRemoteNotifications()
}

希望这会有所帮助...

关于ios - UIApplication.registerForRemoteNotifications() 只能从主线程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391367/

相关文章:

ios - iPad 6.0 中 UINavigationbar 背景色问题 setTintColor

ios - 一旦发现用户的位置,使当前位置出现在 map 上 swift 3

push-notification - 用于聊天等实时应用程序的 Signal R 与推送通知

ios - 点击搜索按钮后,使搜索栏中的取消按钮处于事件状态

ios - 如何解决 iOS 项目的 pod 问题 "CocoaPods could not find compatible versions for pod"?

ios - ios根据superview的中心位置获取图像的x和y坐标

swift - 具有无法覆盖的默认值的文件和行参数

ios - 数据传递失败

ios - 当特定类型的推送通知到达时,如何不允许 iOS 启动应用程序?

iphone - 如何使用每个通知的 header 格式化推送通知?