xcode - scheduled local notification with time ( Everyday at 6AM ) - iphone

I'm using Xcode 4.3.2, How can i set this local notification "dateToFire" everyday at 6AM?
-(void)notification
{
UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
if (!localNotification)
return;
// Current date
NSDate *date = [NSDate date];
NSDate *dateToFire = //Everyday: 6AM;
// Set the fire date/time
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Notification" ];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self notification];
}

Use CalendarComponents to set the hour to 6, and set localNotification.repeatInterval to NSDayCalendarUnit
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:18];
[components setMinute:0];
localNotification.fireDate = [calendar dateFromComponents:components];

Related

Show local notification on particular days [duplicate]

I want to fire a notification at 10PM(irrespective of country) every week. Do i need to use timeZone. Currently I am using below code to fire notification for every week but there how to fire notification exactly at 10PM.
NSDate *date = [NSDate date];
NSDate* myNewDate = [date dateByAddingTimeInterval:7*24*60*60];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = myNewDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
I would think that setting your fireDate to 22:00 on the day you want to get the notification (rather than "a week from now") would do what you want. You'll probably want to use NSCalendar to do that.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:22];
[dateComponents setMinute:0];
fireDate = [gregorian dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];
[dateComponents release];
I made these changes and it worked...
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
//Getting current Hour
NSDateComponents *components = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponents setDay:7];//After 1 week
[dateComponents setHour:22-hour-1]; //Calculating Remaining time for 10PM && "-1" because i am adding 60 min
[dateComponents setMinute:60-minute];
//Adding remaining time to current Time
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];

Local Notification "Everyday at 7:00am" not notifying

I want a notification to go off everyday at 7:00, but it will not go off. I also want it to show in lock screen. Here is all the code I have so far.
-(void)EveryDayNotify
{
NSLog(#"Good Morning Working");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setAlertBody:#"Good Morning! Have a great day!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatInterval = NSDayCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:07];
[components setMinute:0];
localNotification.fireDate = [calendar dateFromComponents:components];
[localNotification release];
}
Try to use this:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
// set components for time 7:00 a.m.
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = [NSString stringWithFormat: #"Good Morning! Have a great day!"];
notification.alertAction = #"go back";
notification.userInfo= #{#"information": [NSString stringWithFormat:#"Some information"]};
notification.repeatInterval= NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Thumbs up if this helped! :D
You need to call
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
before you release localNotification. Your code does all the setup but doesn't actually schedule it, which is why you never get it.
even though this is a very old question i just came here from google and yeah isn't it funny how everyone answers by just copying other peoples answer?
So for all the people like me coming from google, have a laugh at the spelling mistake that mite cost you a minute or two of wondering why it doesn't work.
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
works, but
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
Notiication doesn't...
it's working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[componentsForReferenceDate setDay:27];
[componentsForReferenceDate setMonth:10];
[componentsForReferenceDate setYear:2016];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
// set components for time 7:00 a.m.
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:8];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = [NSString stringWithFormat: #"БЛАГОСЛОВИТЕ ПРОРОКА (с.а.в)"];
notification.repeatInterval= NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
return YES;
}

Issue with canceling UILocaleNotification

I have created a locale notification for my application which fires every day , also in apple developer documentation mentioned that you can fire only 64 notifications , so my question is how can I prevent this limitation ? I mean my notification scheduled that fires everyday per a year , so is this right way to cancel notification and then fires again with scheduled plan ?
- (void)cancelLocalNotification:(UILocalNotification *)notification {
[[UIApplication sharedApplication] cancelLocalNotification:notification;
}
here is my Notification code :
- (void) notification {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: now];
[componentsForFireDate year];
[componentsForFireDate month];
[componentsForFireDate day];
[componentsForFireDate setHour:1];
[componentsForFireDate setMinute:2];
[componentsForFireDate setSecond:1];
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval= NSDayCalendarUnit;
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Read your problem here. it surely solved ur problem Repeating an iOS local notification
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [eventText text];
localNotif.alertAction = #"View";
localNotif.soundName = audios;
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

iOS: Date Components Inquiry

I was wondering how I would edit the dateComps setDay: to go off 3 days before the date set in the picker. I typed in -3, but that doesn't seem to do the trick.
Any help is much appreciated! Here is my code:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:13];
[dateComps setMinute:30];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
When you say you have typed -3, do you mean you used this line?
[dateComps setDay:[dateComponents day] - 3];
This should in fact work.
Also, you can tell the system to fire any next year by just settings the current year to the date components. You can find the current year just the way you found the date components for pickerDate.
After that, you can set the repeatInterval of the local notification to NSYearCalendarUnit, that way it will fire each year.
Here is a code to subtract 3 days from Today(NOW). You can adapt it for your needs:
NSDate *today = [NSDate date];
NSLog(#"NOW:%#",today);
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
todayComps.day -=3;
NSDate *dateMinus3Days = [gregorian dateFromComponents:todayComps];
NSLog(#"3 days before NOW:%#",dateMinus3Days);
Your making it too complicated. DateComponents can be modified and then set to a new date.
I think all you need to do is subtract 3 days from the date returned by the date picker, no?
NSDate *pickerDate = [self.datePicker date]
pickerDate = [ pickerDate dateByAddingTimeInterval:-3 * 24 * 60 * 60 ] ; // add this line
Version using date components:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
{
NSDateComponent * deltaComponents = [ [ NSDateComponents alloc ] init ];
deltaComponents.days = -3 ;
pickerDate = [ pickerDate dateByAddingComponents:deltaComponents ] ;
}
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:pickerDate];
[dateComps setHour:13];
[dateComps setMinute:30];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Use dateByAddingComponents:toDate:options:
// ...
NSDateComponents *offset = [NSDateComponents new];
[offset setDay:(-3)];
NSDate *date3DaysBefore = [calendar dateByAddingComponents:offset toDate:pickerDate options:0];
// ...

iPhone local notification at particular time

I want to fire a notification at 10PM(irrespective of country) every week. Do i need to use timeZone. Currently I am using below code to fire notification for every week but there how to fire notification exactly at 10PM.
NSDate *date = [NSDate date];
NSDate* myNewDate = [date dateByAddingTimeInterval:7*24*60*60];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = myNewDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
I would think that setting your fireDate to 22:00 on the day you want to get the notification (rather than "a week from now") would do what you want. You'll probably want to use NSCalendar to do that.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:22];
[dateComponents setMinute:0];
fireDate = [gregorian dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];
[dateComponents release];
I made these changes and it worked...
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
//Getting current Hour
NSDateComponents *components = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponents setDay:7];//After 1 week
[dateComponents setHour:22-hour-1]; //Calculating Remaining time for 10PM && "-1" because i am adding 60 min
[dateComponents setMinute:60-minute];
//Adding remaining time to current Time
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];