Search code examples
iosswiftuilocalnotification

Stop local notification after specific time period


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.


Solution

  • 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.