I have an app using local notifications there is a total of 64 notifications, which is the limit of local notifications. I need to repeat each notification every week. I know that to set the repeat interval you use:
alarm.repeatInterval = NSCalendarUnit
I have tried using the .WeekOfYear and .WeekOfMonth. Do they repeat the notification every year or month? And I do not know the calendar unit for weekly. Which one can I use to repeat weekly?
Edit:
This is the code I am using to set the notifications.
let notifyAlarm = UILocalNotification()
let component = NSDateComponents()
component.hour = NSUserDefaults.standardUserDefaults().integerForKey("Hour1")
component.minute = NSUserDefaults.standardUserDefaults().integerForKey("Minute1")
component.weekday = 1
notifyAlarm.fireDate = calendar.dateFromComponents(component)
notifyAlarm.timeZone = NSTimeZone.defaultTimeZone()
notifyAlarm.alertBody = NSUserDefaults.standardUserDefaults().stringForKey("Message1")
notifyAlarm.repeatInterval = NSCalendarUnit.WeekdayOrdinal
notifyAlarm.soundName = NSUserDefaults.standardUserDefaults().stringForKey("Sound1")
app.scheduleLocalNotification(notifyAlarm)
I am setting 64 notifications like that at once. But with different dates.
If you want the notification to fire for first time after a week you need to change the fire date.
I use TimeIntervalSinceNow for this, which is in seconds, so 1 week would be around 604000 seconds.
You can use _ to separate numbers for legibility.
alarm.fireDate = NSDate(timeIntervalSinceNow: 604_000)
Maybe a bit clunky but I think its the easiest for those type of notifications. I do something like this to make it easier.
struct NotificationFireDate {
static let nextDay: NSTimeInterval = 85_000
static let nextWeek: NSTimeInterval = 604_000
}
and than use it like so
alarm.fireDate = NSDate(timeIntervalSinceNow: NotificationFireDate.nextWeek)
Repeat interval should be weekOfYear
alarm.repeatInterval = NSCalendarUnit.WeekOfYear
The first repeating notification should fire 1 week after the first (fireDate) notification fired.
For a full list have a look at this (thanks madmik3)
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit
Related
I'm sorry to ask you for something like that, but I've tried this in five different ways and none of them worked. I'm currently working on an app that gives you everyday, when you awake, a little notification with an interesting fact. It takes those facts from an array that is set. I've tried to use UNNotificationCenter, UILocalNotification, etc. but I didn't succeed in writing that function. I would be glad if you could help me out with this code!
So, at 8 am, the App has to
Call the function that gives it a new fact from the array and store it in UserDefaults, lets call it newFact()
Make a notification that appears on 8 am every day, also if the phone is locked
I would be very thankful if you could help me!
Use Below Code
You can call the function here
let fact = self.newfact()
UserDefaults.standard.set(fact,forKey: "fact")
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2017;
dateComp.month = 04;
dateComp.day = 05;
dateComp.hour = 08;
dateComp.minute = 00;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily alarm "
notification.alertBody = fact
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
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 run UILocalNotifications and repeat it every 2 minutes. For this I do:
let reminderNote: UILocalNotification = UILocalNotification()
reminderNote.fireDate = NSDate(timeIntervalSinceNow: 60 * 2)
reminderNote.repeatInterval = NSCalendarUnit.Hour
reminderNote.alertBody = "some text"
reminderNote.alertAction = "View"
UIApplication.sharedApplication().scheduleLocalNotification(reminderNote)
It runs it just one time, later it does not.
I think it's because of this line:
reminderNote.repeatInterval = NSCalendarUnit.Hour
How can I repeat my notifications every 1.5 or 2 hours?
Directly you can not.
Unfortunately repeatInterval can Only be set as TimeUnit Such as Hour, Minute, Second etc.
Eg: Lets say if you want to repeat notification in each 2 minutes, You will need to create 30 notifications that repeats hourly.
firedate sets the time that the notification fires the first time, and repeatInterval is the interval between between repetitions of the notification.
Unfortunately, you can only schedule notifications to repeat at exact intervals defined by NSCalendar constants: e.g., every minute, every hour, every day, every month, but not at multiples of those intervals.
Luckily, to get a notification every 2 minutes, you can just schedule 29 notifications: one right now, one 2 minutes from now, and later one 2 minutes from previous one - up to your 29 notification schedules, and have all repeat every hour. Like so:
So the code in the question schedules a notification to fire every 2 minutes (60 * 2 seconds) from now, and then repeat every hour.
UILocalNotification *reminderNote = [[UILocalNotification alloc]init];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 2];
reminderNote.repeatInterval = NSHourCalendarUnit;
reminderNote.alertBody = #"some text";
reminderNote.alertAction = #"View";
reminderNote.soundName = #"sound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
For 2 hours, you set your notification for now, after 2 hours from now and 2 hours from the previous one up to 11 notification (12*2 hours) = 24 - 2 hour (because 1st one will be on the day change) = 22/2 = 11 notifications will be required to set on the repeat interval of day by NSDayCalendarUnit.
How can I handle date change event in swift?
I want to create a set of notification when the date changes.
var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = " Woww it works!!"
localNotification.fireDate = date
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
You can subscribe to UIApplicationSignificantTimeChangeNotification. This one should fire when the day changes.
Unfortunately this one also fires on other occasions. So you should store a static variable with the date the last time this notification fired and then check if it actually changed the day.
I don't know what you're gonna use it for, but in most cases the general significant time change is what one is looking for.
You could do localNotification.fireDate = NSDate(timeIntervalSinceNow: 1) which will give you a notification 1 day from the current date. This could be further customized to your liking depending on how specific you want the timing to be.
EDIT: As was pointed out in the comments, the timeIntervalSinceNow is calculated in seconds, so 1 would be one second, not one day.
EDIT 2: Trying to answer the question that was asked. If you want to create a notification at midnight every day...
First, create an NSCalendar object
var calendar = NSCalendar()
var calendarComponents = NSDateComponents()
calendarComponents.setDay(29)
calendarComponents.setMonth(6)
calendarComponents.setYear(2015)
calendarComponents.setHour(12)
calendarComponents.setSeconds(0)
calendarcomponents.setMinutes(0)
calendar.setTimeZone(NSTimeZone.defaultTimeZone)
var dateToFire = calendar.dateFromComponents(calendarComponents)
Now we can schedule the notification daily.
localNotification.fireDate = dateToFire
localNotification.setTimeZone(NSTimeZone.defaultTimeZone)
localNotification.setRepeatInterval(kcfCalendarUnitDay)
Syntax might not be perfect, I was translating from Obj-C, but you should get the general idea.
Try adding observer for NSNotification.Name.NSCalendarDayChanged
The down side of this that it triggers only when app is in foreground
When using repeatInteval the notifications keep being pushed one after the other regardless if set on Minutes/Day/Hour etc.
It did seem to work fine until I tested every few seconds now settings won't change back. Any reason why ?
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 15;
dateComp.minute = 27;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily"
notification.alertBody = "OK"
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
If you set the notification to be repeated with CalendarUnitDay it should repeat each day at the same time after the first fire.
Pay attention that to delete scheduled notification is not sufficient to delete the app (at least it was in iOS7) because the system keeps the notification registered but silent for 24h to avoid accidental uninstall.
Maybe you are still seeing old scheduled notifications.
To be sure put a breakpoint and ask the app delegate for its -scheduledNotifications if you find more than you expected this is the source of your problems.
If you tested with every few seconds just once means that all notifications are scheduled and will be received. Try cancel all the scheduled notifications first and then reschedule at desired time interval
UIApplication.sharedApplication().cancelAllLocalNotifications()