How can I trigger a local notification through an IF condition?

So basically what I want to do is trigger a local notification if a variable is below a certain value..


For example:


if myVar < 5 {

// Trigger notification

}


How can I accomplish this?

Replies

If you want to trigger the notification immediately (while the app is running in the foreground or background), create a UNTimeIntervalNotificationTrigger and set the time interval to 0.


Keep in mind that you cannot schedule a local notification to run code if the app is not already running. More info: https://stackoverflow.com/questions/35388086/is-there-a-way-to-wakeup-suspended-app-in-ios-without-user-or-server-interventio

Hey, I don't need any of those things....in my case, the app will already be running but it will be in the background...in my app a certain variable is getting updated while the app is in the background and I just need a notification to pop up once that variable is less than a certain number....is there any way I can accomplish that?

Trigger the local notification like the example:


https://developer.apple.com/documentation/usernotifications/unmutablenotificationcontent

I tried the interval thing already....but the problem is that it recounts the time interval every time the app goes in the background....so like every time the user goes in the background, 5 seconds later there will be a notification....thats not what I want....I just need the notification to go off once, if the variable is less than a certain value

Did you make sure to set repeats to false?

Could you show the code where you create and send notification ?