如何在swift中的UIAlertView中显示推送通知(FCM)的内容?
这link没有帮助,因为它是针对ObjectiveC ...
我希望在收到推送通知(FCM)时向客户端显示具有特定消息的UIAlertView . 例如:
如果(从FCM收到“a”)在UIAlertView中显示“特定消息1”,否则如果(从FCM收到“b”)在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)
}
1 回答
当您尝试显示警报时,是否在控制台中出现任何错误?有时当我从
AppDelegate
展示东西时,我看到这样的东西:我更喜欢找到顶视图控制器并从那里显示警报......但是将调用存在于异步块中也对我有用 .