How to schedule a local notification request with the future start date(with Time) and end date(with Time). is it possible as I am unable to see any variable in the framework
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.dateFormat = "dd MMM yyyy HH:mm"
guard let startDate = formatter.date(from: "03 Sep 2021 09:40")
, let endDate = formatter.date(from: "03 Sep 2021 10:40") else { return }
let intervalBetweenNotifications: Double = 5 * 60 // 5 minutes
for item in stride(from: startDate.timeIntervalSinceNow, to: endDate.timeIntervalSinceNow, by: intervalBetweenNotifications) {
let objectId = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = "Title of the notification"
content.sound = .default
content.threadIdentifier = objectId
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: item, repeats: false)
let request = UNNotificationRequest(identifier: objectId, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
You can change the start, end date and the interval between consecutive notifications.
Related
In my to-do app, I'm trying to set local notification at the time when dead line date for task have been came, but i can't figure out whats wrong with calendar trigger, interval trigger working ok. In function body i put default Date()
func setupNotifications(id: String, contentTitle: String, contentBody: String, date: Date) {
center.getNotificationSettings { (settings) in
if (settings.authorizationStatus == .authorized) {
let content = UNMutableNotificationContent()
content.title = contentTitle
content.body = contentBody
content.sound = .default
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour], from: Date().addingTimeInterval(5))
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let trigger2 = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
let request2 = UNNotificationRequest(identifier: id, content: content, trigger: trigger2)
self.center.add(request)
}
}
You said:
let dateComponents = Calendar.current.dateComponents(
[.year, .month, .day, .hour], from: Date().addingTimeInterval(5)
)
A time interval is reckoned in seconds. But your dateComponents doesn't have any seconds; you've told it to consider only the year, the month, the day, and the hour. And that time is in the past.
You can see this simply by converting your date components back to a date:
let d = Date()
print(d) // 2022-12-01 14:24:02 +0000
let dateComponents = Calendar.current.dateComponents(
[.year, .month, .day, .hour],
from: d.addingTimeInterval(5)
)
let d2 = Calendar.current.date(from: dateComponents)
print(d2) // 2022-12-01 14:00:00 +0000
So at the very least you would need to include .seconds in your list of components. That, as you can see, changes everything:
import UIKit
let d = Date()
print(d) // 2022-12-01 14:25:22 +0000
let dateComponents = Calendar.current.dateComponents(
[.year, .month, .day, .hour, .second],
from: d.addingTimeInterval(5)
)
let d2 = Calendar.current.date(from: dateComponents)
print(d2) // 2022-12-01 14:00:27 +0000
found solution here
https://github.com/YoussifHany51/ToDoList/blob/main/ToDoList/Models/localNotification.swift
adopted to my project
func setupNotifications(id: String, deadline:Date?) {
if deadline != nil {
let calendar = Calendar.current
let hour = calendar.component(.hour, from: deadline ?? Date())
let minute = calendar.component(.minute, from: deadline ?? Date())
let second = calendar.component(.second, from: deadline ?? Date())
let day = calendar.component(.day, from: deadline ?? Date())
let month = calendar.component(.month, from: deadline ?? Date())
let content = UNMutableNotificationContent()
content.title = "To Do"
content.subtitle = "Today is deadline for: \(id)"
content.sound = .default
var operationDate = DateComponents()
operationDate.hour = hour
operationDate.minute = minute
operationDate.second = second
operationDate.month = month
operationDate.day = day
//Trigger
let trigger = UNCalendarNotificationTrigger(dateMatching: operationDate, repeats: false)
//Request
let requst = UNNotificationRequest(
identifier: id, content: content,trigger: trigger
)
UNUserNotificationCenter.current().add(requst)
}
}
enter image description here
I want to send local notifications between 10A.M to 10P.M repeating every 2hours(user input) for the user selected days.
var dateComponents = DateComponents()
dateComponents.hour = 10
dateComponents.minute = 00
let userCalendar = Calendar.current
var dateStart = userCalendar.date(from: dateComponents)
var dateComponent = DateComponents()
dateComponent.hour = 22
dateComponent.minute = 00
var dateEnd = userCalendar.date(from: dateComponent)
while dateStart < dateEnd {
//Add repeating hours start time
UserDefaults.standard.set(self.txtField.text, forKey: "hoursSendary")
let Nohours = UserDefaults.standard.integer(forKey: "hoursSendary")
dateStart = dateStart?.addingTimeInterval(TimeInterval(Nohours * 60 * 60))
//Schedule notification With body and title.
scheduleNotification(body: "Free your eyes", titles: "Screen Alarm",day: 1)
}
func scheduleNotification( body: String, titles:String,day:Int) {
UserDefaults.standard.set(self.txtField.text, forKey: "hoursSendary")
let Nohours = UserDefaults.standard.integer(forKey: "hoursSendary")
let date = Date(timeIntervalSinceNow:TimeInterval(Nohours * 60 * 60) )
var components = DateComponents()
components.weekday = day// sunday = 1 ... saturday = 7
components.weekdayOrdinal = 10
components.timeZone = .current
components.hour = Nohours
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default
content.categoryIdentifier = "ScreenAlarm"
let request = UNNotificationRequest(identifier: "NotificationAt-\(date))", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
How can I pass the user selcted days, I have a collection view with days and I'm doing this stuff inside the didselect method.
I am making a local notification that will run every year, the problem is that I need notifications that come based on the Hebrew calendar, that is, July 20, 2020 according to the Hebrew calendar 28 tamuz 5780 and if we add the year to 28 tamuz 5781 then according to the Gregorian calendar comes out on July 1, 2021, that is, according to the Gregorian calendar, the date changes, I also need to take into account the leap year, AnniversariesReminderService is responsible for the notification, thanks
July 20, 2020 is 28 tamuz 5780
but
28 tamuz 5781 is July 1, 2021
I need to automatically calculate this time difference so that every year I receive a notification according to the Jewish date
struct ReminderAnniversaries {
var id:Int!
var description:String!
var date:Date!
}
class AnniversariesReminderService {
fileprivate var formatter :DateFormatter {
let dateformatterDay = DateFormatter()
dateformatterDay.dateFormat = "yyyy.MM.dd HH:mm"
return dateformatterDay
}
func scheduleReminder(data: ReminderAnniversaries, complited: (() -> Void)? = nil) {
removeScheduledReminder(data: data)
let request = self.prepareNotificationRequest(data: data)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print("ERROR SCHEDULE NOTIFICATION TYPE: \(String(describing: "\(data.id!)")), ERROR: \(String(describing:error?.localizedDescription))")
}
complited?()
})
}
fileprivate func prepareNotificationRequest(data: ReminderAnniversaries) -> UNNotificationRequest {
let content = UNMutableNotificationContent()
content.sound = .default
content.title = "AppDisplayName".localized()
content.body = data.description
let fireDate = data.date!
var dateInfo = DateComponents()
let triggerDaily = Calendar(identifier: .hebrew).dateComponents([.year, .month, .day, .hour, .second], from: fireDate)
dateInfo.year = fireDate.year
dateInfo.month = fireDate.month
dateInfo.day = fireDate.day
dateInfo.hour = fireDate.hour
dateInfo.minute = fireDate.minute + 1
dateInfo.second = fireDate.second
content.userInfo = ["id": data.id, "date" : fireDate,
"description": data.description]
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let requestIdentifier = "\(data.id!)"
let request = UNNotificationRequest(identifier: requestIdentifier,
content: content, trigger: trigger)
print("NOTIFICATION SCHEDULED: (String(describing: requestIdentifier))")
print("NOTIFICATION TITLE: \(String(describing: content.body))")
print("NOTIFICATION TIME: \(String(describing: self.formatter.string(from: fireDate)))")
return request
}
func removeScheduledReminder(data: ReminderAnniversaries) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["\(data.id!)"])
print("NOTIFICATION REMOVED: \(String(describing: "\(data.id!)"))")
}
func getScheduledReminderDate(data: ReminderAnniversaries) -> String? {
var scheduledDate: Date?
let semaphore = DispatchSemaphore(value: 0)
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
notifications.forEach({ notification in
if let scheduledType = notification.content.userInfo["id"] as? Int, scheduledType == data.id {
scheduledDate = notification.content.userInfo["date"] as? Date ?? Date()
// continue;
semaphore.signal()
}
})
if scheduledDate == nil {
semaphore.signal()
}
}
semaphore.wait()
return scheduledDate != nil ? formatter.string(from: scheduledDate!) : nil
}
}
Setting of date
#objc func doneDatePicker(){
if isAlarmClock && stage != 1 {
datePicker.datePickerMode = .dateAndTime
pickerDone.setTitle("Done".localized(), for: .normal)
pickerCancel.setTitle("Back".localized(), for: .normal)
stage = 1
formatter.dateFormat = "dd-MMM-yyyy HH:mm"
} else if !isAlarmClock {
//1
formatter.dateFormat = "dd-MMM-yyyy"
formatter.calendar = Calendar(identifier: .gregorian)
let dateOfDied = formatter.string(from: datePicker.date)
selectedDate = datePicker.date
// result Jule 21 2020 г.
//2
formatter.calendar = Calendar(identifier: .hebrew) //convert to jewish date
formatter.dateFormat = "dd-MMM-yyyy"
jewishDatePicker.date = datePicker.date
jewishDatePicker.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.hebrew)! as Calendar //convert to jewish calendar for adding year
//count of years between date of died and current date
let calendar = Calendar.current
// Replace the hour (time) of both dates with 00:00
let date1 = calendar.startOfDay(for: selectedDate)
let date2 = calendar.startOfDay(for: Date())
let components = calendar.dateComponents([.year], from: date1, to: date2)
var dayComponent = DateComponents()
dayComponent.year = components.year! + 1
let new_date = jewishDatePicker.calendar.date(byAdding: dayComponent, to: jewishDatePicker.date) // adding year by jewish calendar
jewishSelectedDate = new_date!
let hebDate = formatter.string(from: jewishSelectedDate)
jewishDatePicker.locale = Locale(identifier: "he")
jewishDatePicker.setDate(new_date!, animated: false)
print(jewishDatePicker.date)
// result Jule 9 2021 г.
//3
formatter.calendar = Calendar(identifier: .gregorian) //convert to gregorian date
let gregDate = formatter.string(from: new_date!)
// result Jule 09 2021 г.
alarmClock.isEnabled = true
alarmClockBtn()
var txt = Localized("You choosed", arguments: "\(dateOfDied)")
txt = txt + " " + "Jewish anniversary falls on".localized() + " " + "\(gregDate) (\(hebDate))," + " " + "select the date and time to remind you".localized()
text.text = txt
} else {
reminderTime = datePicker.date
pickerView.isHidden = true
}
}
#objc func addNotificationBtn(){
if !(name.text?.isEmpty ?? false) && !(parentName.text?.isEmpty ?? false) &&
jewishSelectedDate != nil && reminderTime != nil {
errorNotifier.notify("Done".localized(), title: "Notification configured".localized(), showOK: true) { [weak self] in
let remind = ReminderService()
var txt = Localized("You have set an anniversary reminder", arguments: self!.name.text!)
txt = txt + " " + "Бен(Бат)".localized() + " " + self!.parentName.text!
let data = ReminderAnniversaries(id: 11, description: txt, date: self?.reminderTime)
remind.anniversaries.scheduleReminder(data: data) {
print("ready")
}
self?.dismiss(animated: true, completion: nil)
}
} else if (name.text?.isEmpty ?? false) && (parentName.text?.isEmpty ?? false) {
errorNotifier.notify("Check the fields".localized(), title: "Fields must not be empty".localized(), showOK: true)
} else if jewishSelectedDate == nil {
errorNotifier.notify("Please select date if died".localized(), title:"", showOK: true)
} else if reminderTime == nil {
errorNotifier.notify("Please select a time for the anniversary reminder, the time is automatically set the day before the death anniversary".localized(), title: "Please select a time reminder".localized(), showOK: true)
}
}
I am trying to get a user to enter the time to take medicine or when they have an appointment in a text field and then send an alarm or notification at that time. I am a newbie so I don't know how to send notifications and such. I would need to do multiple of these depending on how many the user wants.
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default()
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
// Change the time to 7:00:00 in your locale
components.hour = 7
components.minute = 0
components.second = 0
let date = gregorian.date(from: components)!
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
print("INSIDE NOTIFICATION")
UNUserNotificationCenter.current().add(request, withCompletionHandler: {(error) in
if let error = error {
print("SOMETHING WENT WRONG")
}
})
Hi I'm trying to implement date/time notification but the date is 2 hours off.
Code when selecting date
func SelectTime(sender: UIView){
formatter.timeZone = TimeZone.autoupdatingCurrent
let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
picker, userDateWithTime, index in
self.formatter.timeZone = TimeZone.autoupdatingCurrent
self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
self.dateSelected.text = self.formatter.string(for: userDateWithTime)
//print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
return
Code for creating the Notification
let uuid = UUID().uuidString
let notification = UNMutableNotificationContent()
notification.title = "Plus - Todo"
//notification.subtitle
notification.body = taskText.text!
let calendar = Calendar.current
let dateComponents = calendar.dateComponents(in: .current, from: userDateWithTime)
//let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
The problem was that userPickedDateWithTime had a type of Any and not Date. I solved this by creating a formattedDate with the type date and the set the formattedDate = userPickedDateWithTime as! Date and then used the formattedDate instead of userPickedDateWithTime when creating the notification
var formattedDate = Date()
func SelectTime(sender: UIView){
formatter.timeZone = TimeZone.autoupdatingCurrent
let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
picker, userDateWithTime, index in
self.formatter.timeZone = TimeZone.autoupdatingCurrent
self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
self.dateSelected.text = self.formatter.string(for: userDateWithTime)
//print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
formattedDate = userPickedDateWithtime as! Date // <-- This solved the problem
return
Notification code
let uuid = UUID().uuidString
let notification = UNMutableNotificationContent()
notification.title = "Plus - Todo"
//notification.subtitle
notification.body = taskText.text!
let calendar = Calendar.current
let dateComponents = calendar.dateComponents(in: .current, from: formattedDate)
//let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)