首页 文章

swift:在UIAlertView中显示推送通知的内容

提问于
浏览
-2

如何在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 回答

  • 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)
    }
    

相关问题