Local notifications

I want to make a notification that gets triggered by an if statement.

if (value > 10){ //trigger notification}

I am not sure of how to do this, please help.

Answered by Developer Tools Engineer in 677902022

If you wish to schedule a local notification, please refer to
Scheduling a notification Locally from Your App

For the notification to be triggered immediately, please note in the UNNotificationTrigger documentation: "If the request does not contain a UNNotificationTrigger object, the notification is delivered right away." addNotificationRequest

Do you know how to do it when if is not needed?

Not sure I understand.

Do you need to post notification ? If so, that will be like this

let center = UNUserNotificationCenter.current()
center.delegate = self
let requestIdentifier = "RequestID" 
if value > 10 {
        let content = UNMutableNotificationContent()
    // Build the content
    content.categoryIdentifier = "alarm"
   // Then trigger the notification
        UNUserNotificationCenter.current().delegate = self
        self.registerCategories()
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: TimeInterval(1), repeats: false) //set the time interval as needed
        let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
        center.add(request) {(error) in }  
}
Accepted Answer

If you wish to schedule a local notification, please refer to
Scheduling a notification Locally from Your App

For the notification to be triggered immediately, please note in the UNNotificationTrigger documentation: "If the request does not contain a UNNotificationTrigger object, the notification is delivered right away." addNotificationRequest

Local notifications
 
 
Q