在前台推送通知

时间:2015-10-23 13:20:08

标签: ios swift push-notification foreground

我有一个能够接收推送通知的应用程序。

当应用程序处于后台模式时,推送通知显示良好,但如果应用程序处于前台模式,我想向我的custom view显示来自userInfo

如何通过appDelegate viewController通知任何didReceiveRemoteNotification以显示此自定义视图并在那里发送userInfo字典?

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

2 个答案:

答案 0 :(得分:4)

您可以发出通知,然后您想要提醒的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
    }
}

答案 1 :(得分:1)

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

相关问题