ios - 在前台推送通知

标签 ios swift push-notification foreground

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

当应用程序处于后台模式时,推送通知显示良好,但如果应用程序处于前台模式,我想显示我的自定义 View 包含来自 userInfo 的信息。

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

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

最佳答案

您可以发送通知,然后您想要收到警报的 viewController 可以拥有它们的观察者:

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

关于ios - 在前台推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303349/

相关文章:

ios - iOS 5 更新后,我的文档目录中的文件不见了? iOS 是否正在更改文档目录?

ios - 分析 AppStore Connect 中特定应用的 iOS 版本分布

android - 为没有 Cordova 插件的 Android PWA 发送本地推送通知

iphone - 收到推送通知时如何判断我的 iPhone 应用程序是否正在运行?

ios - 如何在 XCode 6.3 iOS 项目中找到权利 plist 文件

iOS App Thinning - 如何根据屏幕尺寸实现它?

ios - http body 的值没有成员请求

ios - 如何确认 ref.removeAllObservers() 已完成?

ios - 如何从 UITextField 键盘中删除模糊

php - Microsoft Azure 通知错误 : 404 No service is hosted at the specified address