我正在开发一个从远程通知接收数据的应用程序,我试图在通过点击通知打开应用程序时将该数据传递didFinishLaunchingWithOptions
给我的ViewController
使用。问题是我的观察者没有得到任何数据。Notificationcenter
launchOptions
viewDidAppear
这是我的didFinishLaunchingWithOptions
方法代码:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
let nameSchool = remoteNotification["name_school" as! String]
NotificationCenter.default.post(name: Notification.Name.nameSchool, object: nameSchool)
}
}
以及方法中的观察者viewDidAppear
:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(forName: Notification.Name.nameSchool, object: nil, queue: OperationQueue.main) { (nameSchool) in
let schoolName = nameSchool.object as! String
self.messagePopup(message: "Data received")
}
}