Send Local Notifications while App is running in the background Swift 2.0

纵饮孤独 提交于 2019-11-29 21:12:39

问题


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).

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.


回答1:


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
    }


来源:https://stackoverflow.com/questions/35008731/send-local-notifications-while-app-is-running-in-the-background-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!