How to schedule a local notification two specific days of the week? - ios

I want to schedule a notification to be fired twice a week:
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
[2, 4].forEach { day in
let content = UNMutableNotificationContent()
content.title = "Notif title"
content.body = "Comment"
content.sound = .default
var components = gregorian.dateComponents([.year, .month, .weekdayOrdinal, .day, .hour, .minute, .second], from: now)
components.hour = 11
components.minute = 25
components.second = 0
components.weekdayOrdinal = day
let date = gregorian.date(from: components)!
let triggerDaily = gregorian.dateComponents([.weekdayOrdinal, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Oops: \(error)")
} else {
print("Notification created!")
}
}
}
But instead of being fired once on monday and once on wednesday, it gets called two times each day.
What am I doing wrong? Thanks for your help.

because of foreach loop your code schedule two notification with the same identifier. while your code is getting execute the 2nd loop cycle overwrite the request.
I did some R&D and refine your code a bit. On my side, it works perfectly.
Please try the code mention below.
func setNotification() {
let days = [2,4]
func ScheduleNotification(index:Int){
let calendarComponents = NSDateComponents()
calendarComponents.hour = 11
calendarComponents.minute = 25
calendarComponents.second = 0
calendarComponents.weekday = days[index]
let content = UNMutableNotificationContent()
content.title = "Notification title"
content.body = "your message here."
content.sound = UNNotificationSound.default
let trigger = UNCalendarNotificationTrigger(dateMatching: calendarComponents as DateComponents, repeats: true)
let identifier = "UYLLocalNotification\(days[index])"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
if let error = error {
print(error)
}else{
if index == 0{
ScheduleNotification(index: 1)
}
}
})
}
ScheduleNotification(index: 0)
}
call this function in your viewdidLoad method like.
self.setNotification()
i hope it will save your time.

Simply change the name of identifier of each notification inside the foreach(_:) to something like test2, test4, i.e.
[2, 4].forEach {
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.sound = .default
var components = DateComponents()
components.timeZone = TimeZone.current
components.weekday = 11
components.hour = 25
components.minute = 0
components.second = $0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "test\($0)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Oops: \(error)")
} else {
print("Notification created!")
}
}
}

You can use .weekday instead of .weekdayOrdinal . According to Apple document
.weekday = A weekday or count of weekdays.
.weekdayOrdinal = weekday ordinal or count of weekday ordinals.
Weekday ordinal units represent the position of the weekday within the
next larger calendar unit, such as the month. For example, 2 is the
weekday ordinal unit for the second Friday of the month.
Try this
let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: date as Date)
triggerDat.weekday = day[index]
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
repeats: true)

Related

how to fire local notification on time in swift?

currently I'm working one project in swift 5. I'm trying to push local notifications on specific date & time. I have tried below code and when I set 60.0 interval then its working find but when I set specific date & time then not working.
I have searched & checked so many question but still didn't found the solution. I have checked below questions
get current date from [NSDate date] but set the time to 10:00 am
UNUserNotificationCenter Swift - Local Notification Not Firing in specific cases
let app = UIApplication.shared
let notificationSettings = UIUserNotificationSettings(types: [.alert, .sound], categories: nil)
app.registerUserNotificationSettings(notificationSettings)
let calendar = NSCalendar.current
let date = NSDateComponents()
print("before date get ::=> \(date)")
date.hour = 16
date.minute = 52
date.month = 6
date.day = 20
date.year = 2019
date.timeZone = NSTimeZone.system
print("after custom date get ::=> \(date)")
let alarm = UILocalNotification()
alarm.fireDate = calendar.date(from: date as DateComponents)
alarm.timeZone = NSTimeZone.default
alarm.alertTitle = "Data success"
alarm.alertBody = "successfuly"
alarm.soundName = "Sound.wav"
app.scheduleLocalNotification(alarm)
app.scheduleLocalNotification(alarm)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil)
content.sound = UNNotificationSound.default
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber;
content.categoryIdentifier = "com.elonchan.localNotification"
// Deliver the notification in 60 seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0 , repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
}
I expect to set specific date & time to fire local notification.
You have to use the UNCalendarNotificationTrigger not UNTimeIntervalNotificationTrigger and UILocalNotificaion is deprecated
let mutable = UNMutableNotificationContent()
mutable.body = "message"
mutable.title = "title"
var date = DateComponents()
date.hour = 16
date.minute = 52
date.month = 6
date.day = 20
date.year = 2019
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
let request = UNNotificationRequest(identifier: "key", content: mutable, trigger: trigger)
UNUserNotificationCenter.current().add(request)
This code will help you on how to send a local notification every day. You can set an hour and minute.
func setUpLocalNotification(_ hour: Int, _ minute: Int) {
print("Local Notification Setup")
let calendar = NSCalendar(identifier: .gregorian)!;
var dateFire = Date()
// if today's date is passed, use tomorrow
var fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire)
if (fireComponents.hour! > hour
|| (fireComponents.hour == hour && fireComponents.minute! >= minute) ) {
dateFire = dateFire.addingTimeInterval(86400) // Use tomorrow's date
fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire);
}
/// -> If you want to get tomorrow date you can use this code.
/*
dateFire = dateFire.tomorrow!
*/
// set up the time
fireComponents.hour = hour
fireComponents.minute = minute
// schedule local notification
dateFire = calendar.date(from: fireComponents)!
let localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "alert body"
localNotification.alertTitle = "alert title"
localNotification.repeatInterval = NSCalendar.Unit.day
localNotification.soundName = UILocalNotificationDefaultSoundName;
UIApplication.shared.scheduleLocalNotification(localNotification);
}
extension Date {
var tomorrow: Date? {
return Calendar.current.date(byAdding: .day, value: 1, to: self)
}
}

iOS How to create schedule local notification by days, weeks, and hourly Swift

I want to create a schedule for users, for now, I know how to create a specific time, but I want to create such notification for example for separate days, or only for a weekend, or for days which user which setup.
My code for hourly notification:
var notifTime = Int()
var notifMin = Int()
func test() {
let content = UNMutableNotificationContent()
content.title = "Test"
content.body = "It's test bro"
content.sound = UNNotificationSound.default()
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
components.hour = notifTime
components.minute = notifMin
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: "any", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
For time pick up i use date picker.
So how I can create schedule notification per days?
let startDate = Date()
let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: startDate)
Set startDate as first notification time date.
Hope it's work for you.

Triggering a weekly notification in swift 3 using local notifications api

I want to trigger a weekly notification that goes on Mondays at 7:00pm.
I see code like this:
let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
What should I set the from date as?
Also does the date need to be in the present?
I am using iOS 10+ and swift 3.
Pass date which you want to generate local notification
let strDate = (data as AnyObject).value(forKey: "not_date") as? String
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd hh:mm a"
let notidate = dateformatter.date(from: strDate!)
you need to first stattnotificationdate like = 2018-06-25 07:00 PM after you need to add time interval
var dateStart = Date()
dateStart = dateStart.addingTimeInterval(TimeInterval(604800))
and generate notification with these interwal
//MARK:- Schedule Notification with Daily bases.
func scheduleNotification(at date: Date, body: String, titles:String) {
let triggerDaily = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
if #available(iOS 10.0, *) {
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "HardikBar"
let request = UNNotificationRequest(identifier: "NotificationAt-\(date))", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
} else {
// Fallback on earlier versions
}
}
I have used below extension for Date:
extension Date {
static func today() -> Date {
return Date()
}
func next(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.Next,
weekday,
considerToday: considerToday)
}
func previous(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.Previous,
weekday,
considerToday: considerToday)
}
func get(_ direction: SearchDirection,
_ weekDay: Weekday,
considerToday consider: Bool = false) -> Date {
let dayName = weekDay.rawValue
let weekdaysName = getWeekDaysInEnglish().map { $0.lowercased() }
assert(weekdaysName.contains(dayName), "weekday symbol should be in form \(weekdaysName)")
let searchWeekdayIndex = weekdaysName.index(of: dayName)! + 1
let calendar = Calendar(identifier: .gregorian)
if consider && calendar.component(.weekday, from: self) == searchWeekdayIndex {
return self
}
var nextDateComponent = DateComponents()
nextDateComponent.weekday = searchWeekdayIndex
let date = calendar.nextDate(after: self,
matching: nextDateComponent,
matchingPolicy: .nextTime,
direction: direction.calendarSearchDirection)
return date!
}
}
This can be used for getting any next or previous weekday from the reference date. You'll need to get the next Monday first and then set the UNCalendarNotificationTrigger as below:
var triggerDaily = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date().next(Date.Weekday.monday, considerToday: true))
triggerDaily.hour = 19
triggerDaily.minute = 0
triggerDaily.second = 0
if #available(iOS 10.0, *) {
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "Test"
let request = UNNotificationRequest(identifier: "NotificationAt-\(String(describing: Calendar.current.date(from: triggerDaily))))", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}else {
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (arrayRequests) in
for request in arrayRequests {
if let calendarNotificationTrigger = request.trigger as? UNCalendarNotificationTrigger,
let nextTriggerDate = calendarNotificationTrigger.nextTriggerDate() {
print("nextTriggerDate ===>>> \(nextTriggerDate)")
}
print("Request ===>>> \(String(describing: request.trigger))")
}
})
}
}
} else {
// Fallback on earlier versions
}
The Output of this will be as below:
nextTriggerDate ===>>> 2018-07-02 13:30:00 +0000
Request ===>>> Optional(
Calendar Year: 2018
Month: 7
Day: 2
Hour: 19
Minute: 0
Second: 0, repeats: YES>)
You can set weekday and time in DateComponents you can receive notification for every monday at 7:00 PM and also set Repeat = "true"
func setupNotificationReminder() {
let title:String = "Your reminder text goes here"
var calendarComponents = DateComponents()
calendarComponents.weekday = 2
calendarComponents.hour = 19
calendarComponents.second = 0
calendarComponents.minute = 0
// create a corresponding local notification
let trigger = UNCalendarNotificationTrigger(dateMatching: calendarComponents, repeats: true)
let content = UNMutableNotificationContent()
content.title = title
content.body = "body"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "YourApp"
let request = UNNotificationRequest(identifier: "NotificationAtGivenTime", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
Another overload of trigger required TimeInterval
So you can get
let timeInterval = dateOfNotification.timeIntervalSinceNow
Now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: true)
You can create date as
if let date = Calendar.current.date(byAdding: .day, value: 7, to: self) {
let date1 = Calendar.current.date(bySettingHour: hours, minute: minutes, second: 00, of: date)
}
If you want specific day then refer https://stackoverflow.com/a/49561764/4601900

How to set the time of start and end of the repeat local notification?

I tried to repeat a notification with UNCalendarNotificationTrigger(dateMatching:, repeats:), but this method can only repeat at a certain time.
I also tried UNTimeIntervalNotificationTrigger(timeInterval:, repeats:) and repeated a notification by a time interval, but this method could not set the start time of push notification.
And these two methods seems that there is no place to set a time for end push notifications.
I want to start with a special time and repeat notifications at regular intervals. What should I do?
Rather than using the repeats parameter you could loop from your start time to end time scheduling individual notifications.
let notifIDPrefix = "mynotif"
let notifCategory = "com.mydomain.mynotif" // this should have been registered with UNUserNotificationCenter
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)
count += 1
}
}
private func scheduleNotif(with identifier: String, date: Date) {
let content = UNMutableNotificationContent()
content.title = "My Title"
content.body = " "
content.categoryIdentifier = notifCategory
content.sound = UNNotificationSound.default()
let triggerTime = Calendar.current.dateComponents([.year, .day, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: false)
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)
}
}
}
The following would schedule 3 notifications (1, 2, and 3 minutes from now).
let startDate = Date().addingTimeInterval(60)
let endDate = startDate.addingTimeInterval(60 * 2)
let interval: TimeInterval = 60
scheduleNotifs(from: startDate, to: endDate, with: interval)

Repeating local notifications for specific days of week (Swift 3 IOS 10)

I am trying to schedule local notifications for specific days of week (e.g. Monday, Wednesday and etc) and then repeat them weekly.
This is how the screen for setting notifications looks:
User can select time for the notification and repeating days.
My method for scheduling single non repeating notification looks like this:
static func scheduleNotification(reminder: Reminder) {
// Setup notification content.
let content = UNMutableNotificationContent()
//content.title = NSString.localizedUserNotificationString(forKey: "Reminder", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: reminder.reminderMessage, arguments: nil)
content.sound = UNNotificationSound.default()
// Configure the triger for specified time.
//
let dateComponentes = reminder.dateComponents
// TODO: Configure repeating alarm
// For the testing purposes we will not repeat the reminder
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponentes, repeats: false)
// Create the request object.
let request = UNNotificationRequest(identifier: "\(reminder.reminderId)", content: content, trigger: trigger)
// Schedule the request.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error: Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
The date components are extracted from UIDatePicker widget and stored in reminder class:
let date = reminderTimeDatePicker.date.addingTimeInterval(60 * 60 * 24 * 7)
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date)
...
reminder.dateComponents = components
I have an array selectedDays[Int] (as a property of reminder class) to hold info on days of week on which the notification should fire.
How can I schedule notification on specific day of week and how to repeat it weekly?
Even a single comment will be helpful, and thank you in advance.
You can use below function to get Date from selected picker value:
//Create Date from picker selected value.
func createDate(weekday: Int, hour: Int, minute: Int, year: Int)->Date{
var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.weekday = weekday // sunday = 1 ... saturday = 7
components.weekdayOrdinal = 10
components.timeZone = .current
let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: components)!
}
//Schedule Notification with weekly bases.
func scheduleNotification(at date: Date, body: String, titles:String) {
let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "todoList"
let request = UNNotificationRequest(identifier: "textNotification", 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)")
}
}
}
After getting a value from picker pass picker hour, minute and year with selected week day as (Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, thursday = 5, Friday = 6, Saturday = 7) to function func createDate(weekday: Int, hour: Int, minute: Int, year: Int) to get notification fire date on weekly bases, and after getting date call function func scheduleNotification(at date: Date, body: String, titles:String) for schedule a notification.
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
arguments: nil)
content.categoryIdentifier = "TIMER_EXPIRED"
let weekdaySet = [6,5]
for i in weekdaySet {
var dateInfo = DateComponents()
dateInfo.hour = 16
dateInfo.minute = 44
dateInfo.weekday = i
dateInfo.timeZone = .current
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}

Resources