Is there anyway to show the notification with swift every 15 seconds ? I checked that via
notification.fireDate=NSDate(timeIntervalSinceNow: 15)
but it doesn't work everytime it just showed once , how we can do it as a loop ?
You can't schedule a notification every 15 second. The minimum time between notifications it is 1 minute which it is also very unlikely needed.
var repeatInterval: NSCalendarUnit { get set }
Description The calendar interval at which to reschedule the
notification. If you assign a calendar unit such as weekly
(NSWeekCalendarUnit) or yearly (NSYearCalendarUnit), the system
reschedules the notification for delivery at the specified interval.
Note that intervals of less than one minute are not supported. The
default value is 0, which means that the system fires the notification
once and then discards it.
So just set it up as follow:
notification.repeatInterval = .Minute
You can set localNotification.repeatInterval property which as to be of type NSCalendarUnit
Related
I need to schedule local notifications which I'd like to repeat each day from lets say at 23:00. But I do not want to start from today but from tomorrow.
If I schedule them using UNCalendarNotificationTrigger:
var dateInfo = DateComponents()
dateInfo.hour = 23
dateInfo.minute = 0
dateInfo.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
then the notifications will be received each day at 23:00 but will get notification for today as well if the above code is executed before 23:00.
Is there any good way to achieve that or I just should schedule 64 (the iOS limit for undelivered local notifications) different local notifications with exact dates?
My current project is a timer which uses a date picker to set the amount of time the user wants before the timer goes off (say 1 minute, 6 hours and two minutes etc.). The problem lies in the amount of time that the date picker believes it has been set for. Below is the code which I am using to set the time.
#IBOutlet weak var datePicker: UIDatePicker!
var timeAmount:Double = 0
#IBAction func startButton() {
timeAmount = Double(datePicker.countDownDuration)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeAmount, repeats: false)
}
Here it can be seen that the function startButton, sets the value of timeAmount to be the amount of time that the date picker is set for. This timeAmount value is then used in a local notification as the time.
The issue is that the datePicker.countDownDuration is never correct. If it is set for one minute, then timeAmount may return a value of 99 seconds, 62 seconds, 67 seconds etc. It is just completely random.
Maybe I do not entirely understand how the .countDownDuration feature works, however from everything I have read, this should return a value of 60 seconds.
Any thoughts and suggestions on the matter will be very much appreciated, thank you.
By default, the UIDatePicker will take the current second value for the calculation.
timeAmount = (number of minutes selected in picker * 60) + current time second value.
For example:
If the current time is 13:40:25 (1 PM 40 minutes 25 seconds) and you have selected one minute in the date picker, the value of timeAmount in this case is 85.
timeAmount = (1*60) + 25
This will solve your problem.
Go to your Storyboard and select UIDatapicker. Navigate to Date option in Attributes inspector.
Click the Dropdown and change it to Custom from Current Date.
You will see a new text field with the current time as the custom date.
Just change the second field to 00.
Run the App now. Now it will take 0 as the value for the second and you will able to see seconds value correctly based on the time you are choosing in the date picker.
Hope this will solve your problem.
I'm trying to get my app to call a function at specific time intervals. For example, I might want the function to be called every hour on the hour, so at 1:00 AM, 2:00 AM, and so on. I have tried doing this with an NSTimer, but I find that it has trouble staying in sync when resuming after the machine sleeps or is powered off. Is there a way for my app to detect when we have reached a specific date and time and to call a function at that time? Thanks.
You could try Grand Central Dispatch. Specifically use dispatch_walltime() to create a dispatch_time_t representing the time you want the job to run and then use dispatch_after() to submit the job to Grand Central Dispatch for execution at the specified time.
Run it every minute and test whether ≥ 1h has elapsed since the last invocation.
You could use a helper method similar to below in combination with the NSTimer. The timer could fire its selector function every sec/minute/etc in which you pass this helper a pair of currentDate/endDate and when the returned value is <= 0 then execute your timed event function once with a flag and move your endDate forward an hour.
func timeBetween(currentDate: NSDate, endDate: NSDate) -> Double
{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Second], fromDate: currentDate, toDate: endDate, options: [])
return Double(components.second)
}
My application generates multiple local notifications and while I'm testing it, I see that the app continuously receives the local notification appDelegate message. This is in spite of my having no repeat interval.
<UIConcreteLocalNotification: 0x1574b0>{fire date = (null), time zone = (null), repeat interval = 0, next fire date = 2011-09-23 19:40:35 +0000}
This is the debug value returned at my breakpoint. As you'll notice, there is no data except the next fire date, which shouldn't technically be called because there is no interval. Also, this is the only notification in the SharedApplication LocalNotifications array.
i am New to iphone Development .I Am Trying To Use NslocalNotification In My Project I Need To Give Remeinder For Every 2Hours or For Every Two Days Or For Every Two Months Etc..Currently I am Using NslocalNotification Repeat Interval .But Its Working For Only Every Minute For Every Hour using Nscalender ....
NSString *InterVal=[freQuencyArr objectAtIndex:index-2];
NSString *InterValType=[freQuencyArr objectAtIndex:index-1];
if(![InterVal isEqualToString:#"Every"])
{
result=[InterVal intValue];
}else
result=1;
if([InterValType isEqualToString:#"Day"]){
notification.repeatInterval= NSDayCalendarUnit;
}else if([InterValType isEqualToString:#"Week"]){
notification.repeatInterval= NSWeekCalendarUnit;
}
else if([InterValType isEqualToString:#"Month"]){
notification.repeatInterval= NSMonthCalendarUnit;
}else if([InterValType isEqualToString:#"days"]){
notification.repeatInterval=result*24*60*60;
}
here If result is 2 depend Up on IntervalType I Need Notification
its Not Working With Me
if([InterValType isEqualToString:#"days"]){
notification.repeatInterval=result*24*60*60;
}
#Srinivas:
If you look at the link I have posted in this answer, You will come to know that I have tried every possible solution here to try and do what you want currently.
I had tried all this to implement it in my app, but this doesn't work.
I am afraid to say this but this is not possible. It only allows the unit NSCalendarUnit objects to be set as a repeat interval.
I invested almost 2 months (I asked the question in Dec 2010 and answered it myself in February 2011) to try and implement every possible solution available on internet through different articles and different forums but none did help.
Check out my link and lookout for all the answers if something is useful to you.
How to set Local Notification repeat interval to custom time interval?
Really Hope that this helps you.
The repeatInterval property of a UILocalNotification cannot be used to repeat less than every one calendar unit, i.e. every day, every week, every month, etc.
Instead, you will have to schedule multiple notifications to achieve the desired effect, setting the fireDate property accordingly.
As lemnar says you are unable to use repeatInterval to repeat in a frequency different from the calendar units Apple provided. So, the code below:
if([InterValType isEqualToString:#"days"]){
notification.repeatInterval=result*24*60*60;
}
Will not do anything. I am also using repeat notifications in an app that I have built and the way I've gotten around this is by creating multiple notifications each repeating to give the "desired" repeat frequency. As an example, if I want to repeat "every 2 days", I can't do this using repeatInterval. However, I have a "scheduling function" in my app that creates multiple individual notifications to achieve this. I do this going out an arbitrary length of time (in my case, one week). So in the example above, when the user specifies that he / she needs a notification every two days from today, I create 3 notifications (one each for day 3, 5, and 7).
For repeating at a frequency less than a calendar unit, things are a little easier. Say I need to repeat every 12 hours (at 6AM and 6PM). Then, I would create 2 notifications (one for 6AM and another for 6PM). I would then set the repeatInterval for each of these notifications to NSDayCalendarUnit. This way I have created a set of notifications that repeat every 12 hours.
When my app loads, I go out another 7 days and recreate notifications as needed. Not the most elegant solution, but this was the best way I could think of getting around the repeatInterval limitation.