0
votes

I am using local notifications in an app. I am triggering local notification after every 1 hour, 2 hour, etc. I want to stop this notification after 8 hours. The code I am using to trigger local notification is bellow:-

func localNotificationFire(timeInterval: Int) {
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
        if error != nil {
            print("Request authorization failed!")
        } else {
            print("Request authorization succeeded!")
            
           let content = UNMutableNotificationContent() // Содержимое уведомления
            
            content.title = ""
            content.body = kNotificationReminder
            content.sound = UNNotificationSound.default
            content.badge = 1
            
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timeInterval), repeats: true)
            let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger)
            // 4
            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
    } 
}

I know, I can stop this notification like UNUserNotificationCenter.current().removeAllPendingNotificationRequests() But I want to automatically stop this notification.

1
You can create 8 notifications at once with UNCalendarNotificationTrigger, just set the exact datetime. Bare in mind that you can have about 20 local notifications at one time in UNUserNotificationCenter. Obviously it is not the best solution, but I counldn't find better. - Tiran Ut
I can't do that because I want to trigger the notification after every specific interval until specific time - iOS Developer
Notifications will either continue indefinitely (or until you cancel them, but you need to be running in the foreground to do that) or you can schedule the required number of individual notifications as @TiranUt suggested - Paulw11
Thanks for the clarification, So that means there is no direct option to stop local notification when it set to repeat - iOS Developer

1 Answers

0
votes

So as per @Paulw11 and @TiranU answer, I scheduled the required number of individual notification. The solution is as follows:-

func localNotificationFire(timeInterval: Int, totalHours: Int) {
    let num = totalHours/timeInterval
    for i in 0..<num {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
            if error != nil {
                print("Request authorization failed!")
            } else {
                print("Request authorization succeeded!")
                
                let content = UNMutableNotificationContent() 
                
                content.title = ""
                content.body = kNotificationReminder
                content.sound = UNNotificationSound.default
                content.badge = 1
                
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval((i+1)*timeInterval), repeats: false)
                let request = UNNotificationRequest(identifier: "notification.id.\(i)", content: content, trigger: trigger)
                // 4
                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
            }
        }
    }
}

Where total hours is the time till I want to trigger the notification and timeInterval is the time gap to trigger a notification.