How can I schedule a local notification that fire every Wednesday and Saturday at noon local device time?
let localNoonSatNotification:UILocalNotification = UILocalNotification()
localNoonSatNotification.userInfo = ["uid":"noonBreak"]
localNoonSatNotification.alertAction = "Noon Break"
localNoonSatNotification.alertBody = "Time for a break! Come and play few levels"
localNoonSatNotification.fireDate = // get next Wednesday/Saturday 12:00 PM
localNoonSatNotification.soundName = UILocalNotificationDefaultSoundName
localNoonSatNotification.applicationIconBadgeNumber = 1
UIApplication.sharedApplication().scheduleLocalNotification(localNoonSatNotification)
If someone still needs help with this question UNCalendarNotificationTrigger
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.weekday = 3 // Tuesday
dateComponents.hour = 14 // 14:00 hours
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)
You can set repeat interval,
notification.repeatInterval = NSCalendarUnit.CalendarUnitWeekday
Hope this will help :)
Related
I can't seem to find any Apple Documentation for this exact scenario, and I've tried various ways to do this and I keep coming up empty.
I would like to schedule a repeating notification (iOS 10+ so UNCalendarNotificationTrigger or equivalent).
These are Local Notifications not Push Notifications.
My Goal:
Schedule notifications that repeat:
once a fortnight (e.g., every second Tuesday)
once a quarter (e.g., 1st of every 3 months)
My Current Approach:
These triggers work well, and are simple to implement (Running the code in a Swift Playground).
// Every day at 12pm
var daily = DateComponents()
daily.hour = 12
let dailyTrigger = UNCalendarNotificationTrigger(dateMatching: daily, repeats: true)
dailyTrigger.nextTriggerDate() // "Jan 4, 2017, 12:00 PM"
// Every Tuesday at 12pm
var weekly = DateComponents()
weekly.hour = 12
weekly.weekday = 3
let weeklyTrigger = UNCalendarNotificationTrigger(dateMatching: weekly, repeats: true)
weeklyTrigger.nextTriggerDate() // "Jan 10, 2017, 12:00 PM"
// The 1st of every month at 12pm
var monthly = DateComponents()
monthly.hour = 12
monthly.day = 1
let monthlyTrigger = UNCalendarNotificationTrigger(dateMatching: monthly, repeats: true)
monthlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM"
// Every 1st of February at 12pm
var yearly = DateComponents()
yearly.hour = 12
yearly.day = 1
yearly.month = 2
let yearlyTrigger = UNCalendarNotificationTrigger(dateMatching: yearly, repeats: true)
yearlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM"
But...
I can't seem to get a fortnightly or quarterly trigger to function correctly.
// Every second Tuesday at 12pm
// ... There is no "date.fortnight", is this possible?
// The 1st of every quarter at 12pm
var quarterly = DateComponents()
quarterly.hour = 12
quarterly.day = 4
// Values: 1, 2, 3 or 4 all produce the same "nextTriggerDate" - why?
quarterly.quarter = 4
let quarterlyTrigger = UNCalendarNotificationTrigger(dateMatching: quarterly, repeats: true)
quarterlyTrigger.nextTriggerDate()
So, to be clear, my questions are:
Is it possible to get a notification that repeats every fortnight?
How do we get a trigger for once a quarter?
Since DateComponents() has a quarter unit, I assume that a quarterly trigger is possible. For the fortnightly reminder however, I'm not even sure if this is possible...
Any insight would be appreciated!
I didn't see any direct option to trigger fortnight notification.Suggestion from my end.
1) Is it possible to get a notification that repeats every fortnight?
I am proposing two options:
Can we use UNTimeIntervalNotificationTrigger to repeat the notification with a time interval of two weeks time? Example below
let timeInterValFor2Weeks = 1190507.790425003
let intervalTrigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterValFor2Weeks, repeats: true)//"Jan 3, 2017, 5:24 PM"
intervalTrigger.nextTriggerDate() //"Jan 17, 2017, 12:05 PM"
Schedule two UNCalendarNotificationTrigger trigger, which should trigger first and third day of a month. For example it should fire notification in first Sunday and third Sunday of a month.
var fortnightPart1 = DateComponents()
fortnightPart1.weekday = 1 //(Day..here Sunday)
fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
fortnightPart1.hour = 12
let fortnightTrigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: true)
fortnightPart1.nextTriggerDate()
2) How do we get a trigger for once a quarter?
If there is no direct option available, then I suggest the same solution as above.
I've been trying to schedule local notifications by weekday, but they seem to be firing immediately instead of when I schedule them. This is my code in AppDelegate in didFinishLaunchingWithOptions:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
var notification = UILocalNotification()
var components = NSDateComponents()
var calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
components.weekday = 5
components.hour = 9
components.minute = 24
notification.alertBody = "Notification test"
notification.fireDate = calendar?.dateFromComponents(components)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Any clues on why it might not be working?
As others have mentioned, make sure to specify the year and month properties of the components object.
components.year = 2016
components.month = 6
Part of the current fire date of your code is the following:
0001-01-01
Which means the notification will be fired at year "1" in January, not in 2016.
You need to use notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
Try to set a year/month because it is created from 01/01/1970, so the date is in the past => immediate trigger
I want to set notification at the time (hour, minute) that I set. But It show error:
func notification(story: Story) {
let dateComponents = NSDateComponents.init()
dateComponents.weekday = 5
dateComponents.hour = story.remindeAtHour
dateComponents.minute = story.remindeAtMinute
let notification = UILocalNotification()
notification.alertAction = "Title"
notification.alertBody = "It's time to take a photo"
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.fireDate = dateComponents.calendar
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
As Paul w points out in his comment, the error message is telling you what's wrong.
You need to set the notification's fireDate property to a date (An NSDate). You need a method that will convert date components to an NSDate. How about the NSCalendar method dateFromComponents:?
I am trying to create a weekly recurring local notification using Swift. Notification is going to be set to first day of every week, and in 10:00. Ofcourse I don't want to set a notification for a past date, so I need to see if now it has passed first day of week 10:00. If not, I will create notification for today 10:00, else next week monday 10:00.
I created an extension to calculate week number for current date.
extension NSDate {
func weekOfYear() -> Int {
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.WeekOfYear, fromDate: self)
let weekOfYear = components.weekOfYear
return weekOfYear
}
}
I couldn't go any further than this. Any help is appreciated.
Regards.
There is an easier solution to your problem. You can use the nextDateAfterDate() method of NSCalendar:
let cal = NSCalendar.currentCalendar()
let comps = NSDateComponents()
comps.hour = 10
comps.weekday = cal.firstWeekday
let now = NSDate()
let fireDate = cal.nextDateAfterDate(now, matchingComponents: comps, options: [.MatchNextTimePreservingSmallerUnits])!
This gives the first date in the future which is at 10:00 on the first day of the week.
Hi, I am trying to make a push alert in swift that goes off every morning at 7 AM local time (not GMT).
Here's my code:
func scheduleLocalNotification() {
var localNotification = UILocalNotification()
localNotification.timeZone = timeZone
//localNotification.fireDate = Here is where I need help.
localNotification.alertBody = "FooBar"
localNotification.alertAction = "BarFoo"
}
I cannot seem to figure out how to achieve code that sends a push notification every morning at 7 AM local time, rather than just once at 7:00 AM. How do I do this? Is there a NSDate object that can do this? Different code all together?
Thanks!
Use the repeatInterval field to set up a repeat and use NSCalendar to calculate the next 7 AM
let calendar = NSCalendar.currentCalendar()
// Calculate the next 7 AM
var date = calendar.dateBySettingHour(7, minute: 0, second: 0, ofDate: NSDate(), options:nil)
if date?.timeIntervalSinceNow < 0 {
date = calendar.dateByAddingUnit(.CalendarUnitDay, value: 1, toDate: date!, options: nil)
}
localNotification.fireDate = date
// Set up a daily repeat
localNotification.repeatInterval = .CalendarUnitDay
localNotification.repeatCalendar = calendar