iPhone : Daily local notifications - ios

I am trying to implement local notification
This is what I have set
// Current date
NSDate *date = [NSDate date];
// Add one minute to the current time
NSDate *dateToFire = [date dateByAddingTimeInterval:20];
// Set the fire date/time
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
Instead of 20, I want to put a fixed time(daily)to start push.
For ex:I want to push notification pop up at every 6:00AM.
How can do that ?
Thanks

You just need to properly create a NSDate object to be your fire date (time). Instead of using [NSDate dateByAddingTimeInterval: 20], use something like this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
Here are the Apple NSDateComponents API docs
And then when you add the date to the notification, set the repeat interval to one day:
[localNotification setFireDate: dateToFire];
[localNotification setTimeZone: [NSTimeZone defaultTimeZone]];
[localNotification setRepeatInterval: kCFCalendarUnitDay];
As with all date related code, make sure to test how this works during the switch to daylight savings time, if your time zone uses daylight savings time.

I guess what you need is NSDayCalendarUnit.
You can check this answer. And here is another tutorial worth reading.

NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"Glass.aiff";
notifyAlarm.alertBody = #"Staff meeting in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}

Related

UILocalNotification with repeatInterval set NSWeekdayCalendarUnit doesn't trigger weekly

I'm trying to setup local notification to repeat weekly.
Here's my setup:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSWeekdayCalendarUnit;
In console log:
localNotif: {fire date = Friday, April 24, 2015 at 12:27:33 PM Singapore Standard Time, time zone = Asia/Singapore (GMT+8) offset 28800, repeat interval = NSWeekdayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, April 25, 2015 at 12:27:33 PM Singapore Standard Time, user info = {
KUserLocalNotficationKey = "2015-04-24 04:27:33 +0000";
}}
As you can see, the next fire date is trigger on the next day. Is this a bug?
I've tested it in iOS 7.1, 8.1, 8.3.
you have to code like this
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSCalendarUnitWeekOfYear;
It trigger notification on weekly bases.
PLz Try this
UILocalNotification *localNotif=[[UILocalNotification alloc]init];
localNotif.fireDate =currentDate;
localNotif.timeZone=[NSTimeZone defaultTimeZone];
localNotif.alertBody = #"MazeRoll";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval=NSWeekCalendarUnit;
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotif];
NSLog(#"sdfsdfsdf%#",[[UIApplication sharedApplication] scheduledLocalNotifications]);
You can set local notification with NSCalendarUnitWeekOfYear and setting up proper fire date.
UILocalNotification *localNotification=[[UILocalNotification alloc]init];
localNotification.fireDate =[NSDate date]; // set your week day on which repeatedly you want local notfication. for example Monday, then set Fire date of next monday.
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.repeatInterval=NSCalendarUnitWeekOfYear;
NSLog(#"%#",localNotification);
Hope this helps. Enjoy Coding !!
NSDate *currentDate = [NSDate date];
NSDate *futureTime = [currentDate dateByAddingTimeInterval:60*60*168];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:futureTime];
if ([components hour] >= 19) { // make it the next day
[components setDay:[components day] + 1 ];
}
[components setHour:8];
[components setMinute:00];
NSDate *alertTime = [calendar dateFromComponents:components];
NSLog(#"alertTime %#",alertTime.description);
UILocalNotification *localNotif=[[UILocalNotification alloc]init];
localNotif.fireDate =alertTime;
localNotif.timeZone=[NSTimeZone defaultTimeZone];
localNotif.alertBody = #"MazeRoll";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval=NSDayCalendarUnit;
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotif];
NSLog(#"sdfsdfsdf%#",[[UIApplication sharedApplication] scheduledLocalNotifications]);

How to fire daily localnotification at specific time in iOS

I want to get daily notification and for that I have googled and
I got some solution from this iPhone : Daily local notifications but I couldn't recognise properly
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
Let's say suppose current time is 3:13, and current date is 20-11-2014
in this I want to set local notification at time of 3:14, 20-11-2014 can anybody please help me, because I have tried with following things but not working
[components setDay: 20];
[components setMonth: 11];
[components setYear: 2014];
[components setHour: 15];
[components setMinute: 14];
[components setSecond: 0];
NSDate *date = [NSDate date];
NSDate *newDate1 = [date dateByAddingTimeInterval:60*60*24];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = newDate1;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [[NSUserDefaults standardUserDefaults] objectForKey:#"NotificationText"];
localNotif.alertAction = #"View";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
try this one hope this may help you. Thank you
NSDate* now = [NSDate date] ;
NSDateComponents* tomorrowComponents = [NSDateComponents new] ;
tomorrowComponents.day = 1 ;
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDate* tomorrow = [calendar dateByAddingComponents:tomorrowComponents toDate:now options:0] ;
NSDateComponents* tomorrowAt7AMComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow] ;
tomorrowAt7AMComponents.hour = 7 ;
NSDate* tomorrowAt7AM = [calendar dateFromComponents:tomorrowAt7AMComponents] ;
localnotification.fireDate = tomorrowAt7AM;
I hope this helps you.

Local notifications are repeat on every day and every month

I have a Local Notifications. Like this ;
NSNumberFormatter *faturaSonOdemeGunuFormatter = [[NSNumberFormatter alloc] init];
[faturaSonOdemeGunuFormatter setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *faturaSonOdemeGunuNumber = [faturaSonOdemeGunuFormatter numberFromString:_txtFaturaGunu.text];
NSDate *currentDate = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:currentDate];
[components setTimeZone:[NSTimeZone systemTimeZone]];
[components setHour:12];
[components setMinute:40];
[components setDay: faturaSonOdemeGunuNumber.integerValue -3];
NSDate *test = [calendar dateFromComponents:components];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = test;
localNotification.alertBody = [NSString stringWithFormat:#"%# Faturanıza 3 gün kaldı.",_txtFaturaAdi.text];
localNotification.alertAction = #"Faturayı göster";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
This code does not repeat itself.I want to repeat Local Notification every Month in faturaSonOdemeGunuNumber integerValue day.It should be start sonOdemeGunuNumber -3 (for example user writes 17. it should start 14) and it should give a notification on every day until faturaSonOdemeGunuNumber. I mean it should continue notify the users every day until selected day.
How can i do that?
Thank you!
Add this:
localNotification.repeatInterval = kCFCalendarUnitMonth; //For monthly repeats
localNotification.repeatInterval = kCFCalendarUnitDay; //For daily repeats
Try this,
notification.repeatInterval= NSDayCalendarUnit; //For day
notification.repeatInterval= NSWeekCalendarUnit; //For Week
notification.repeatInterval= NSMonthCalendarUnit; //For Month
Ref Link: http://useyourloaf.com/blog/2010/09/13/repeating-an-ios-local-notification.html

Calling A Method At Specific Time Every Day

My app runs constantly in kiosk mode. Once every 24 hours at a specific time I need to sync some data from core data to a web service.
I know how to do the sync piece but I don't know how to schedule the app to make the sync call at a specific time each day e.g. at 02:45 am.
Is it possible to do something like this when an app is running constantly?
Use Local Notifications. Here is a tutorial:http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/
Hope this helps u to start...
This as well:
Local Notifications
Background Tasks
Figured this out thanks to prompts from #lakesh. Posting solution in the case it helps somebody because I found NSNotification examples very difficult to understand at first.
In my main view controller I added the following method:
- (void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 02];
[components setMinute: 45];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
This sets a fire date for the notification of today at 02:45 am and recurs daily.
In viewDidLoad in my view controller I call the above method:
[self scheduleNotification];
In the appdelegate I do the following:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
//call your method that you want to do something in your app
}
A simple solution is just to set NSTimer to check current date every minute (or every second, for example). If the current date is greater then required, then fire the method and update the required date.

How to allocate custom time in UILocalnotification?

- (void)scheduleNotification :(int) rowNo
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.timeZone = [NSTimeZone defaultTimeZone];
NSString *descriptionBody=[[remedyArray objectAtIndex:rowNo]objectForKey:#"RemedyTxtDic"];
NSLog(#"%#",descriptionBody);
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
forKey:#"kRemindMeNotificationDataKey"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
I have a column name frequency which is fetched from Sqldb where it contains the number of times notification should appear for a particular cell.
if frequency = 3 ..the notification should fire say 8 AM , 2PM then 8PM
if frequency = 4 ..the notification should fire say 8 AM , 12PM , 4PM then 8PM.
Is there a way to do it? If anyone can help me that would be great
Unfortunately, You can specify value for repeatInterval only of type NSCalendarUnit (day, week, month). So, I think, You need to create several Notifications with different fireDate, and specify for them repeatInterval = NSDayCalendarUnit
For example,
NSDate *currentTime = [NSDate date];
notification1.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL sinceDate:currentTime];
notification1.repeatInterval = NSDayCalendarUnit;
notification2.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL * 2 sinceDate:currentTime];
notification2.repeatInterval = NSDayCalendarUnit;
After user viewed some of notifications - You can cancel them.
Update.
You can also create NSDate from different components like this:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2013];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
Also You can set hour, minutes, seconds, timezone and other parameters.

Resources