在前台推送通知

push notification in foreground

我的应用程序能够接收推送通知

当应用程序处于background模式时推送通知显示良好,但如果应用程序处于foreground模式我想显示我的custom view 以及来自 userInfo 的信息。

如何从我的 appDelegate didReceiveRemoteNotification 通知任何 viewController 显示此自定义视图并向那里发送 userInfo 字典?

有人可以帮我解决我的问题吗?

你可以发送一个通知,然后你想要被提醒的 viewControllers 可以有观察者:

NSNotificationCenter.defaultCenter().postNotificationName("nameOfNotification", object: yourVariableContainingUserInfo)

你能像这样添加观察者吗:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "nameOfNotification:", name: "nameOfNotification", object: nil)

你的选择器看起来像这样:

func nameOfNotification(notification: NSNotification) {
    if let dict = notification.object as? NSDictionary {
        //user your userInfo dictionary here
    }
}

您可以使用 NSNotificationCenterdidReceiveRemoteNotification 通知您的 viewcotroller。然后,您可以根据需要使用在 NSNotificationCenter 方法中传递的 userinfo 字典显示自定义视图。