如何在swift中的UIAlertView中显示推送通知(FCM)的内容?
此link没有帮助,因为它适用于ObjectiveC ...
我希望在收到推送通知(FCM)时向客户端显示具有特定消息的UIAlertView。例如:
如果(" a"从FCM收到)在UIAlertView"中显示"特定消息1,否则如果(" b" 从FCM收到)显示" UIAlertView"中的特定消息2。
我刚试过这个,但它对我不起作用:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// display alert view when notification is received !!!
let alertController = UIAlertController(title: "Alert", message: "you have a new notification", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
答案 0 :(得分:0)
当您尝试显示警报时,是否在控制台中出现任何错误?有时候,当我从AppDelegate
呈现内容时,我会看到类似的内容:
2017-01-01 16:04:14.806 wylt[46457:5781064] Warning: Attempt to present <UIAlertController: 0x7faf13f0a910> on <wylt.WYLTNavigationViewController: 0x7faf1402e200> whose view is not in the window hierarchy!
我更喜欢找到顶视图控制器并从那里显示警报......但是将调用放在异步块中也对我有用。
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}