ios - 如何在 Swift3 的 UIViewController 中获取 FCM 消息?

标签 ios swift swift3 firebase-cloud-messaging

我正在使用 swift3。当我的应用程序收到 FCM 消息数据时,我想在 UIViewController 中向 tableview 显示 FCM 消息。我有我的 fcm 数据在 appDelegate 中使用以下代码..但我不知道如何在 UIViewController 中获取数据。

func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {

    let message : UNNotificationPresentationOptions = .alert

    //GlobalVariables.notification_message = getAlert(notification: .alert)


    if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing.
        print("active****")

        print("\(notification.request.content.userInfo)")
        completionHandler([message, .sound])

    } else {
        print("Not active****")
        completionHandler([message, .badge, .sound])
    }
}

请帮帮我 提前致谢

最佳答案

在您的 AppDelegate 上设置一个变量,然后使用它:

  • AppDelegate 在 window?.rootViewController 上的 didFinishLaunchingWithOptions
  • 你的 View Controller 通过调用 (UIApplication.shared.delegate as?AppDelegate)?.yourVariable

我建议在使用后将 AppDelegate 中的变量设置为 nil,以防止使用相同的通知数据两次。

关于ios - 如何在 Swift3 的 UIViewController 中获取 FCM 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207329/

相关文章:

ios - 如何在同一个 View Controller 中使用多个搜索 Controller ?

swift - "Fatal error: Unexpectedly found nil while unwrapping an Optional value"是什么意思?

ios - MBProgressHUD 没有隐藏 - Swift 3

ios - applicationReceivedRemoteMessage 只在前台执行

swift - 在扩展 swift 中使用枚举作为字符串

ios - 在自定义类下拉 Xcode 8.3.2 中找不到自定义 Controller 类

iOS 暗模式 - 图像 Assets 未重绘

ios - 在 UIWebView 中显示带有 URL 的 Flash 文件 : Obj C/swift

objective-c - 方法声明中的语法错误 : Expected ')' before name

swift - 永远不会使用不可变值 X 考虑用 Swift 2 中的 '_' 错误替换