-1

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]; 
Community
  • 1
  • 1
nipa
  • 91
  • 2
  • 7
  • Get the current date/time, add one day (if before midnight), extract the date components, set the hour to 7 and reset minutes/seconds to 0, and then create a new date object from those date components. You cannot do it with date components alone. – trojanfoe Nov 20 '14 at 10:08

2 Answers2

2
 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

Deepesh V R
  • 104
  • 5
-1
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.

Dev
  • 363
  • 2
  • 9
  • Don't you need to do something to `tomorrowComponents` before using it? – trojanfoe Nov 20 '14 at 10:16
  • I don't see how. You create an `NSDateComponents` object and use it to create a new date, from *now*, without having set anything in that date components object. – trojanfoe Nov 20 '14 at 10:26
  • 1
    This is't the answer, and u r mislead others. This isn't a `UILocalNotification`, the @Deepesh V R is the right answer... – gran33 Nov 20 '14 at 10:34
  • localnotification.fireDate .here the localnotification refers to the UILocalNotification object – Dev Nov 20 '14 at 10:38
  • @trojanfoe tomorrowComponents is used to just get a date(tomorrow's) from 'now' from the calendar. it has not been used after that – Dev Nov 20 '14 at 10:42
  • But it's not been initialised. I would expect `tomorrowComponents.day = 1;` before it was used. – trojanfoe Nov 20 '14 at 10:43
  • OK so now it's been updated to what I would expect and I will revert my downvote, but why the statement "no need. It works for me.!!" and why was the answer accepted before this edit? – trojanfoe Nov 20 '14 at 10:46
  • He didn't ask for how to set local notification. he asked for how to set a date for a local notification. He asked for the date. The answer which is marked correct will fire notification after 24hrs. It will not fire notification at 7am daily. – Dev Nov 21 '14 at 05:40