I can set a UNNotification to fire at a specific date/time, like so
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: fireTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
And I can set one to fire at a specific time on a specific day of the week, every week, like so
var components = Calendar.current.dateComponents([.weekday], from: nextMondayDate)
components.setValue(10, for: .hour)
let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerComps, repeats: true)
But is there any way I can set a UNNotification to fire at a specific date and time, and then to repeat every week on that weekday and time?
Specifically, I would like to set a notification to fire every Monday at 10am except next Monday.
if you just want to skip only next monday, you can calculate next to next monday date and set in the components on the place of nextMondayDate keeping rest of the setting as it is, like repeat true.
Related
I have to configure the UIDatePicker to only allow time selection for the next 24 hours from current time.
This is how I set up the picker.
self.timePicker.minimumDate = Date()
self.timePicker.maximumDate = Calendar.current.date(byAdding: .hour,
value: 24,
to: Date())
self.timePicker.datePickerMode = .time
But the time picker is not behaving correctly. If I select a time before the current time, it changes the date to tomorrow as it should. But then after that it does not let me select a time later than current time.
I am confused. Could anyone please help on this
I'm trying to make an application to help quitting smoking,
I need the user to enter his first cigarette time, and the time between each cigarette , and finally the added time to increase time between each cigarette. and at this time I send a local notification.
Ex: let say his first cigarette at 7:00 AM and he smokes 1 cigarette every 1 Hr and he want to add extra 10 min.
so 1st cigarette : 7:00
2nd : 8:10/
3rd : 9:20/
4th :10:30 .. etc
I tried a lot of solutions but I have some problems about date format that I got from picker views, I need them to be like "hh:MM a"
Any help?
To get a date in a different format, you can create a NSDate from a Date object and then use NSDateFormatter to format:
let date=NSDate(timeInterval: 0, since: yourDate)
let dateFormatter=DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm a")
String dateString=dateFormatter.string(from: date)
You can get the initial date object from the UIDatePicker or when it changes and a method is called in its delegate.
first create a date object with
let date = Date(timeIntervalSinceNow: 3600) // it may be any date whatever you want
To create the trigger from the date components:
let triggerDaily = Calendar.current.dateComponents([hour,.minute,.second,], from: date)
Now pass that date in UNCalendarNotificationTrigger with repeats true
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
Create a date with time :
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let someDateTime = formatter.date(from: "2016/10/08 22:31") // put your date and time here
if Locale specific time :
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
Well the solution suggested isn't elegant enough, so you have to create one notification for every hour, instead use this trigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (60*60) + (10*60), repeats: true)
60*60 being the hour you mentioned and 10*60 being the 10 minute, set repeating to true and viola, your notification now triggers every 70 minutes.
Currently working on an App need set different type of repeat rule of notification
Like repeat everyday, workday, weekday or several days of the week.
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
dateInfo.weekday = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
Using this API can only add one day of the week at each time,
is there any simple way.
appreciated
I am currently making an alarm clock app and I am currently stuck at possibly just a misunderstanding. I want to launch a local notification at a specific time and repeat at a time interval. I have been getting a couple ideas and this (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) program is close to what I want it to be but I want it to go off at a certain time and then repeat at a single interval.
After user inputs information, the program will spit out:
timeIntervalSeconds, the interval that user wants the notifications in (in seconds)
timeStart, when the notifications will start
timeEnd, when the notifications will end if the user doesn't stop them
Any suggestions would be greatly appreciated.
Thank you!
To launch notification on specific time use below code.
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
// Change the time to 10:00:00 in your locale
components.hour = 10
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)
And to repeat notifications on specific time interval use below code in trigger.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true) // it repeats after every 2 minutes
Since Apple provide only limited list of intervals for auto repeat of notifications you can try to do next to use custom intervals:
When a notification got fired you should calculate date & time for next notification and just schedule it on desired time.
I am using following code to trigger repeat alarm with end date.
let components = calendar.dateComponents([.month,.day,.hour,.minute], from: date, to: TODate)
After this code I have used notification for trigger. But this code is not working for triggering notifications.
But following code (only from date) works perfectly.
let components = calendar.dateComponents(in: .current, from: date).
How can I schedule a repeat alarm with end date?