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?
Related
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.
I am trying to make a reminder app and I am having a hard time trying to set a start date for my reminders. I am able to make local Notification work with the code below. With this code, I am able to get local notification every Tuesday at the time set by the user (Indicated by the "hour" and "minute").
if tuesdayIsChecked == true {
AlarmSupportFileX.tuesdaySelectedBool = true
hour = hourDateFormatter.string(from: datePicker.date)
minute = minuteDateFormatter.string(from: datePicker.date)
var dateComponents = DateComponents()
dateComponents.weekday = 3
dateComponents.hour = Int(hour)
dateComponents.minute = Int(minute)
print(dateComponents)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let requestIdentifier = "\(randomGeneratedString)Tuesday"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
//Handle error
})
}
My issue is that I only want it to trigger on the Date chosen by user.
Example: If I use my code above on 12th Nov 2018 (Monday), a notification will trigger on 13th Nov 2018 (Tuesday) and all the Tuesday that follows.
Assuming user select alarm to trigger on tuesday (dateComponents.weekday = 3).If user set a notification on 12th Nov(Monday) and they selected a start date on 25th Nov(Sunday), I want it to only show the notification on 27th Nov(Tuesday) and all the Tuesday that follows. How can I achieve this?
This is not possible using only UNCalendarNotificationTrigger.
You would need to set up some sort of mechanism for the app to set up that notification the week of the Tuesday.
One way to do this could be:
check if the selected date is this week
-> IF TRUE: register notification
-> IF FALSE: write to UserDefaults the date.
In an didBecomeActive() or viewDidLoad() you could then load the date from UserDefaults and check if the date is in this week, if that's the case, you register the notification.
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 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.
currently im using this code snippet to add an event to the calendar
let date = NSDate()
let calendar = NSCalendar.current
let hour = calendar.component(.hour, from: date as Date)
let minutes = calendar.component(.minute, from: date as Date)
self.addEventToCalendar(title: "lol", description: "lal", startDate: date, endDate: date)
which is not refined, but it works. I have 2 questions:
is there a way to use the same form for adding an event to the iOS calendar as you see when iOS recognises a date in your e-mails?
is there a way to directly transform a string in to a date? I get the date from the backend like: 31.12.2016 22:30:00 (DD.MM.YYYY HH:MM:SS).