0

I am using two date pickers to get start time and end time, but this will only trigger on the current date , but I want to trigger it daily.Will that be possible to do.

func scheduleNotifs(from startDate: Date, to endDate: Date, with interval: TimeInterval ) {
    var curDate = startDate
    var count: Int = 0
    while curDate.compare(endDate) != .orderedDescending {
        scheduleNotif(with: "\(notifIDPrefix)_\(count)", date: curDate)
        curDate = curDate.addingTimeInterval(interval * 60 * 60)
        count += 1
    }
}

public func scheduleNotif(with identifier: String, date: Date) {
    self.getUser()
    
    let content = UNMutableNotificationContent()
    content.title = "\(username),Want to be more healthy and fresh?"
    content.body = "Water/Tea time"
    content.categoryIdentifier = notifCategory
    content.sound = UNNotificationSound.default
    
    let notification = UILocalNotification()
    notification.repeatInterval = NSCalendar.Unit.day
    
    let triggerTime = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
}

1 Answers1

1

This is example to Post Local Notification with custom date, title and text

import Foundation
import UserNotifications

class LocalNotificationsManager: NSObject {

    static let shared = LocalNotificationsManager()
    
    
    func add(date: Date, title: String, text: String){
        let center = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = title
        content.body = text
        content.sound = .default
        //content.userInfo = ["value": "Data with local notification"]
        
        let fireDate = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: fireDate, repeats: false)
        let request = UNNotificationRequest(identifier: "LOCAL_NOTIFICATIONS_JOBS", content: content, trigger: trigger)
        center.add(request) { (error) in
            if error != nil {
                print("Error = \(error?.localizedDescription ?? "error local notification")")
            }
        }
    }
}

To call this method:

LocalNotificationsManager.shared.add(date: //your date object from picker,
                                     title: "...",
                                     text: "...".localized)

Good Luck

Sergey Burd
  • 171
  • 1
  • 2
  • 15
  • I have two date pickers with start time and end time.Want to trigger notification between thoses times daily. @SergeyBurd – githma amarasinghe Jul 28 '22 at 14:43
  • https://stackoverflow.com/a/45716411/5023088 Get array of all dates between start and end date from Picker And after that trigger Local Notification on one of the date from array – Sergey Burd Jul 29 '22 at 10:28