YungGoat
YungGoat

Reputation: 143

How do I create a notification trigger? [Swift]

How do I create a trigger to fire a notification only if a variable is less than a certain value?

public static var updateElap = Int() // This is the variable I am checking

override func viewDidAppear(_ animated: Bool) {

    NotificationCenter.default.addObserver(self, selector: #selector(appWentInBackground), name: .UIApplicationDidEnterBackground, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)
}

@objc func appWentInBackground(){
    beginDate = Date()
    focusTimer.invalidate()
    print(updateElap)
    print("background")

    /** Notification **/

    let content = UNMutableNotificationContent()
    content.title = "blah"
    content.body = "blah"
    content.badge = 1

     // This is where I need to set the notification
     // if updateElap < blah blah {fire notification} else {blah blah}
     // It's basically a notification inside a notification

}

@objc func appWillEnterForeground() {
    runProdTimer()
    endDate = Date()
    secondsPassedInBackground = endDate.timeIntervalSince(beginDate) // Double
    print(beginDate)
    print(endDate)
    print(secondsPassedInBackground)
    updateElap = updateElap + Int(secondsPassedInBackground)
    print(updateElap)
    print("foreground")
    intBrSeconds = intBrSeconds - Int(secondsPassedInBackground) // Updated Break Time after user returns to the app aka foreground
    breakSession.text = brTimeString(bTime: TimeInterval(intBrSeconds))

}

Is there a way to accomplish this using UNMutableNotificationContent() , UNNotificationRequest() and UNUserNotificationCenter() ?

Upvotes: 0

Views: 268

Answers (1)

Sharad Paghadal
Sharad Paghadal

Reputation: 2154

var myVariable = 1 {
    didSet {
        if myVariable > blah blah {
            //add notification
        }
    }
}

In above case whenever your myVariable value is fall in given criteria, you will have an notification for that.

Update: You have to set this didSelect property where your actual myVariable is declared.

Upvotes: 1

Related Questions