0

I am working on a timer application that should terminate if the user leaves the app. I want a notification to be sent to the user when they leave the application. This notification WARNS the user that the timer will terminate if they do not return to the app immediately I have look at UNNotficationTrigger's but not of what i have tried repeats the notification if the user is to be warned more than once.

How to i detect when the user is outside the application, then send a notification warning them to return to the app?? Thank you in advance

Code:

 @IBAction func start(_ sender: Any) {


    startTimer()    

    let content = UNMutableNotificationContent()

    content.title = "WARNING: Return to 4ocus"

    content.subtitle = ""

    content.body = "Go back to 4ocus or risk losing 4ocus points"

    content.badge = 1



    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)



let request  = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

 @IBAction func start(_ sender: Any) {

    startTimer()

    let content = UNMutableNotificationContent()

    content.title = "WARNING: Return to 4ocus"

    content.subtitle = ""

    content.body = "Go back to 4ocus or risk losing 4ocus points"

    content.badge = 1

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)


let request  = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Chris A.P
  • 11
  • 2

1 Answers1

0
func applicationWillTerminate(_ application: UIApplication) {

    let content = UNMutableNotificationContent()

    //adding title, subtitle, body and badge
    content.title = "Hey if you will not come back"
    content.subtitle = "your timer will be killed"
    content.body = ""
    content.badge = 1

    //getting the notification trigger
    //it will be called after 5 seconds
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    //getting the notification request
    let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)

    //adding the notification to notification center
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

and also put the same code in willforeground method of appdelegate to get know if the user is about to leave application

   func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

put this method in your appdelegate and if there is alredy func applicationWillTerminate(_ application: UIApplication) is defined then put only code :)

Mahesh Dangar
  • 806
  • 5
  • 11
  • @Anuraj Im still having issues, the local notification will appear once when i initially run the application, but when i return to the app, the timer stops , if i exit the app in the same run session, the notification does not appear. – Chris A.P Aug 07 '18 at 10:46
  • @Christopher-BabafijimiAdeogun have implemet delegate methods of UserNotification like willPresent and all ?? – Mahesh Dangar Aug 07 '18 at 10:49
  • @Anuraj Do you mean, imported the method 'import UserNotifications'? if so, then yes. – Chris A.P Aug 07 '18 at 10:55
  • @Anuraj I have placed the code in the 3 app delegates... DidEnterBackground, applicationwillenterforeground and application will terminate. The notifications seem to be working now. Thanks. Do you know how i could set a time threshold to give the user a certain number of seconds to return to the application before the apptimer stops? – Chris A.P Aug 07 '18 at 11:08
  • @Christopher-BabafijimiAdeogun yes you can set background timer on didEnterBackground for some minutes and if user come back within that time period you can set off that timer in didEnterForegroundMethod. and if this answer help you then kindly mark as correct my post – Mahesh Dangar Aug 07 '18 at 11:19