kalgi bhavsar
kalgi bhavsar

Reputation: 21

Local notification cancel for particular time period

I want to fire local notification on a particular time based just liked water notification. Like user can set wake up time & go to bed time n drink water notification for every 2 hours so how can i set that between goto bed to wake up time my local notification won;t fire, other than that the notification will fire for every 2 hours..

Please help me. Thanks

Upvotes: 0

Views: 250

Answers (1)

Julius Bryan
Julius Bryan

Reputation: 83

What you can do is to obtain the hours in between the wake up time and sleep time with the frequency of what you wish your notification to fire off.

Firstly, you would need to function to create a local notification that repeats during a particular hour everyday.

func createNotification(title: String, body: String, hour: Int, repeats: Bool, identifier: String) {
    
    //calendar
    let calendar = Calendar.current
   
    let content = UNMutableNotificationContent()
   
    content.title = title
    content.body = body

    let today = Date()
    
    let futureDate = calendar.date(byAdding: .hour, value: hour, to: today)
    
    let hourComponent = calendar.dateComponents([.hour], from: futureDate!)
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: hourComponent, repeats: repeats)
    
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    
    center.add(request, withCompletionHandler: {(error) in })
}

Secondly, place this code in your viewDidLoad() or the place you wish to set the notifications. What I am doing here is obtaining the wakeup hour and sleep hour and then finding the number of notifications that has to be made using the frequency that I want by dividing the difference in hour with the frequency.

I end of by creating a for loop that creates the notification based on the time that I add to the wakeup hour. eg. First notification fires of at 6am + 1x2 = 8am, second notification fires of at 6am + 2x2 = 10am

    //calendar
    let calendar = Calendar.current
    
    //set the wakeup and sleep hour
    let wakeupHour = 6 //6am
    let sleepHour = 18 // 6pm
    
    //Obtain starting time using date components
    let wakeup = DateComponents(calendar: calendar, hour: wakeupHour)
    
    //Obtain ending time using date components
    let sleep = DateComponents(calendar: calendar, hour: sleepHour)
    
    //calculate the number of hours between the two time
    
    //obtain number of hour difference
    let hourDifference = calendar.dateComponents([.hour], from: wakeup, to: sleep)
    
    //how many hours do you want
    let frequency:Int = 2
    
    //how many notifications will there be between the wakeup and sleeptime
    let numberOfNotifications:Int = hourDifference.hour! / frequency
    
    //create the notifications
    for num in 1...numberOfNotifications {
        //hour that each notification will fire off
        let hour = wakeupHour + frequency * num
           createNotification(title: "Drink Up", body: "Drink Up", hour:  hour, repeats: true, identifier: "drink\(hour)")
    }

I'm still learning swift so this may not be the most efficient way of solving this problem but I hope this helps!

Upvotes: 1

Related Questions