我可以在这个方法中获取推送通知消息:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}
但问题是如何在应用程序运行时以与应用程序处于后台时相同的方式显示警报通知。
这可能吗?
最佳答案
当应用程序在前台时,你只会得到回调,iOS 不会在这种情况下显示警报,你必须自己做...
你可以这样做:
为'PN前景 View '创建一个nib,例如:
当你在前台得到一个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/