应用程序在后台Swift 2.0中运行时发送本地通知 [英] Send Local Notifications while App is running in the background Swift 2.0
问题描述
当用户最小化应用程序时(通过单击iPhone的主页按钮或同时锁定手机),我试图向用户发送推送通知样式警报。
I am trying to send the user a 'Push Notification style' Alert when the user minimizes the app (by clicking on the iPhone's Home Button or by locking the phone as well).
我的应用程序不断解析XML文件(每10秒钟),我希望应用程序继续运行,以便在满足某些条件后向用户发送本地通知在我的程序中,即使在用户最小化应用程序或锁定他们的手机之后。
My app continuously parses an XML file (every 10 seconds) and I want the app to continue running so that it sends the user a Local Notification once some condition has been met in my program, even after the user has minimized the app or locked their phone.
我从教程中反弹并且每个人似乎都安排通知,但是这对我不起作用,因为我的通知不是基于时间的,而是基于满足条件。
I've bounced around from tutorials and everyone seems to 'schedule' a Notification, but this isn't going to work for me because my Notification isn't time-based, rather it's based off conditions being met.
到目前为止我做了什么:
What I've done so far:
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
return true
}
MapViewController.swift
// Some function
func someFunction(delta: Int) {
if delta < 100 {
// Send alert to user if app is open
let alertView = UIAlertController(title: "This is an Alert!", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertView, animated: true, completion: nil)
// Send user a local notification if they have the app running in the bg
pushTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("pushNotification"), userInfo: nil, repeats: false)
}
}
// Send user a local notification if they have the app running in the bg
func pushNotification() {
let notification = UILocalNotification()
notification.alertAction = "Go back to App"
notification.alertBody = "This is a Notification!"
notification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
警报在应用程序打开时效果很好,但是当我最小化手机上的应用程序时,通知永远不会显示。我假设应用程序在后台运行时没有运行,或者我不太了解这个概念。非常感谢任何帮助。
The Alert works great while the app is open, but the notification never shows up when I minimize the app on my phone. I assume the app isn't running while it's in the background or I don't understand this concept that well. Any help is greatly appreciated.
推荐答案
默认情况下,NSTimer仅在模拟器中起作用。尝试将其添加到AppDelegate文件中:
By default NSTimer does work only in simulator. Try to add this to your AppDelegate file:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)
return true
}
这篇关于应用程序在后台Swift 2.0中运行时发送本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!