var topVC: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
topVC?.rootViewController = UIViewController()
let alert = UIAlertController(title: "Alert",message: "Notification Received",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK",style: .cancel) { _ in
// action OK
})
topVC?.makeKeyAndVisible()
topVC?.rootViewController?.present(alert,animated: true,completion: nil)
,
尝试一下:
func showAlert() {
var alertController = UIAlertController(title: "Confirm",message: "Confirm?",preferredStyle: UIAlertControllerStyle.alert)
var okAction = UIAlertAction(title: "Yes",style: UIAlertActionStyle.Default) {
UIAlertAction in
// action
}
var cancelAction = UIAlertAction(title: "No",style: UIAlertActionStyle.Cancel) {
UIAlertAction in
// action
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController,completion: nil)
}
,
我认为您是从错误的地方打电话给func showAlert()
。
当应用程序从通知水龙头启动时,该应用程序会在其中获取事件
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
所以您应该尝试一下
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Handle remote notification event on app launch
if let remoteNotification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] {
showAlert()
}
}
本文链接:https://www.f2er.com/3105485.html