ios - 当应用程序处于前台时显示通知警报

标签 ios swift push-notification

我可以在这个方法中获取推送通知消息:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
} 

但问题是如何在应用程序运行时以与应用程序处于后台时相同的方式显示警报通知。

这可能吗?

最佳答案

当应用程序在前台时,你只会得到回调,iOS 不会在这种情况下显示警报,你必须自己做...

你可以这样做:

为'PN前景 View '创建一个nib,例如:

enter image description here

当你在前台得到一个PN时,你可以实例化 View ,并把它添加到带有动画的UIWindow中,代码例如:

        // init and configure your custom PN foreground view
        let nib = UINib(nibName: self.nibName, bundle: NSBundle(forClass: PnForegroundView))
        pnForegroundView = nib.instantiateWithOwner(nil, options: nil)[0] as! PnForegroundView
        pnForegroundView.setWidth(UIScreen.width)
        pnForegroundView.setHeight(63)
        pnForegroundView.title = <some title from notification>
        pnForegroundView.image = <some image from notification>

        // add the view to the key window 
        let window = UIApplication.sharedApplication().keyWindow!
        window.addSubview(pnForegroundView!)

        // Change window level to hide the status bar
        window.windowLevel = UIWindowLevelStatusBar

        // Show the PN foreground view with animation:

        self.pnForegroundView!.setBottom(0)
        self.changeWindowLevelToHideStatusBar()
        UIView.animateWithDuration(0.2) {
            self.pnForegroundView!.setBottom(self.pnForegroundView!.height)
        }

当然,您应该为此 View 设置一个代理,以防用户点击通知以及用户关闭通知。

此外,您还可以添加自动解雇的时间。

最后一件事 - 删除 PnForegroundView 时,您最好将 UIWindow 级别重置为默认值,以显示状态栏

关于ios - 当应用程序处于前台时显示通知警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37547726/

相关文章:

ios - 为什么 Swift 不将此 Int 数组文字转换为正确的类型?

iOS 位置推送服务扩展。 CLLocationPushServiceErrorDomain代码: 1

ios - 如何以编程方式获取应用组名称?

javascript - 对于推送通知,websocket 是强制性的吗?

ios - 我可以删除重复的 6.0 iOS DeviceSupport 吗?

ios - 如何从线程更新 UI

ios - 在 swift 上编写 UIButton 程序

ios - arc4random (mac os x) 有多随机? (或者我做错了什么?)

android - 使用 cordova 的 FireBase 推送通知

关闭应用程序时未收到 Android FCM 推送通知