0

I'm using backgroundTaskIdentifier to get the stopwatch to continue when the app is closed. The counter updates fine when the app is closed.

But I would like to receive my popup alert notification when the app is closed. Right now it pops up when I open the app but that doesnt help me because the timer has already stopped.

Related question. Can an app have push notifications if nothing is being pushed from a server? I mean can I badge my app when the timer is complete?

Variable declaration

var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?

ViewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateTimerBasedViews", name: Timer.notificationSecondTick, object: timer)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "timerComplete", name: Timer.notificationComplete, object: timer)

backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
    })

Applicable functions

func updateTimerBasedViews() {
    updateTimerLabel()
    updateProgressView()
}

func timerComplete() {
    switchToPickerView()
    playAlarm()
    // TO DO : ADD UIAlert

    let alert = UIAlertController(title: title, message: "Time to do it!", preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    //self.presentViewController(alert, animated: true, completion: nil) // old

    presentViewController(alert, animated: true) { () -> Void in
        let localNotification:UILocalNotification = UILocalNotification()
        localNotification.alertAction = "Testing"
        localNotification.alertBody = "Hello World!"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }

AppDelegate

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
TokyoToo
  • 926
  • 2
  • 10
  • 21

1 Answers1

1

If you want to show popup not in your application but on your device background task with showing UIAlertAction is not right way. It will show bubble in your application context, not the os system.

Your approach should be in using local notifications

You should realize smth like this:

application.registerUserNotificationSettings(UIUserNotificationSettings (forTypes: UIUserNotificationType.Alert, categories: nil))

var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing"
localNotification.alertBody = "Hello World!"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

You can find a lot of tutorial, like this

katleta3000
  • 2,484
  • 1
  • 18
  • 23
  • Thanks. I will mark as correct. You are getting close to 1000 :) I just realized I needed to distinguish between app, local and push notifications. Will this give me a red badge and a alert popup even if the app is closed? Can I put this in the same function above? I thought many people put this code in AppDelegate? Also should I choose between OS or app popup? Having both is not good right? If the app is open it doesn't make sense to have a local notification. – TokyoToo Nov 28 '15 at 07:29
  • I think, that you should experiment with this, I mean, as I understand you want to send local notifications after the background task is completed. Maybe you should call `registerUserNotificationSettings` not in background task. But it looks like the right way to solve your problem – katleta3000 Nov 28 '15 at 07:35
  • put this code `application.registerUserNotificationSettings` in didFinishLaunching and the other code in you background task completion handler – katleta3000 Nov 28 '15 at 07:36
  • It works. I'm getting local notifications but not a badge or sound, only the banner. I have edited my code to show you. What do I need to do? Oh one thing. I'm getting this warning. `Presenting view controllers on detached view controllers is discouraged`. – TokyoToo Nov 28 '15 at 08:04
  • read answer to this question http://stackoverflow.com/questions/4861131/updating-ios-badge-without-push-notifications (add `localNotification.applicationIconBadgeNumber = 1`) – katleta3000 Nov 28 '15 at 08:06
  • your warning - http://stackoverflow.com/questions/19890761/warning-presenting-view-controllers-on-detached-view-controllers-is-discourage (first of all try to google it - practically everything is well known and you can get answer quickly by yourself) – katleta3000 Nov 28 '15 at 08:07