NSNotification before 2 days from targeted date - iphone

I want to fire a notification before 2 days from targeted date.Currently I am using below code to fire notification but there how to fire notification exactly before 2 days from targeted date.
NSString *frstdate = [[calenderarray objectAtIndex:k] objectForKey:#"date"];
NSLog(#"frstdate..%#",frstdate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:frstdate];
NSLog(#"date..%#",date);
NSDate *dateToFire = [[[NSDate alloc] initWithTimeInterval:-24*60*60 sinceDate:date]autorelease];
NSLog(#"dateToFire..%#",dateToFire);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:#"Get Food"], #"foodItem", nil] ;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Thanks in advance.

You should use NSCalendar and NSDateComponents to do the maths - something like:
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* twoDaysAgo = [[NSDateComponents alloc] init];
twoDaysAgo.day = -2;
NSDate* dateToFire = [calendar dateByAddingComponents:twoDaysAgo toDate:date options:0];

You can do arithmetic with calendar dates (year/month/day/hour... representation in a given timezone) via the NSCalendar class. Although in most cases, "24*60*60" seconds will be one day -- users probably won't notice the difference.

Related

how to remove second from nsdate

I'm working with local notification. But while I set time for notification, its also consider second.
This is the code where I set time.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm"];
[tfTime setText:[NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]]];
selectedDateTime = [datePicker date];
I want notification on rounded time, without second.
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
//[dateFormat setDateFormat:#"dd-MM-yyyy"];
//NSDate *notificationDate = [dateFormat dateFromString:strRemindMeBefore];
notification.fireDate = selectedDateTime;
notification.alertBody = tvMessage.text;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
A convenient method to "round" a date to minutes or other units is the rangeOfUnit:…
method of NSCalendar.
NSDate *selectedDateTime = [datePicker date];
NSDate *roundedDate;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSCalendarUnitMinute startDate:&roundedDate interval:NULL forDate:selectedDateTime];
// …
notification.fireDate = roundedDate;
What you can do is use the NSDateComponents as follows and set the seconds to 00, which might help... the following is a sample code where you can set all the components manually!
Hope this helps
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:2013];
[comps setHour:10];
[comps setMinute:10];
/// You can set this component as 00
[comps setSecond:00];
localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:comps];

Notify in specific date and time

So I need help from you: I have to set the code notify in specific date and time. Please help me to fix my code.
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 6];
n1.alertBody = #"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 15];
n2.alertBody = #"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];
It seems you just don't know how to create an NSDate to a specific time. Use NSDateFormatter to convert strings to dates.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *myDate = [df dateFromString:#"2014-04-05 23:30"];
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = myDate;

Local Notification issue iphone

I want to make local notification that notify user 5 times a day and repeat them daily,
I have them in a mutable array which object is "hh:mm" which hours and minutes are fixed for GMT+3 town, so I get the current date and find the interval then create a date for the notification
that's the method I implement.
-first applying time zone,
-second if the time before current time so make it for the next day.
-third set local notification for that date.
plz help me
piece of code
with the use of this sample code i have schedule the 2 notification one at Morning 7AM and one at Evening 6PM and repeated it daily and it works superfine hope you can find out your solution with the use of this.
#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:#"ParsingResponse"];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:7];
[components setMinute:0];
NSDate *today7am = [calendar dateFromComponents:components];
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = today7am;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatCalendar = [NSCalendar currentCalendar];
notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Morning"];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Morning", #"key", nil];
notif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components2 setHour:18];
[components2 setMinute:0];
NSDate *today6pm = [calendar2 dateFromComponents:components2];
UILocalNotification *notif2 = [[cls alloc] init];
notif2.fireDate = today6pm;
notif2.timeZone = [NSTimeZone defaultTimeZone];
notif2.repeatCalendar = [NSCalendar currentCalendar];
notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Evening"];
notif2.alertAction = #"Show me";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:#"Evening", #"key", nil];
notif2.userInfo = infoDict2;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}

Setting local notification for a future date for Eastern time zone

I'm working on adding local notifications to an app I'm developing. I'm setting just one notification for 11:00pm on April 30, 2013 NY/Eastern time. How would I do this? I've tried multiple methods but none of them have worked correctly. This is what I'm using at the moment (it doesn't work):
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:#"setNotify"]]) {
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"setNotify"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *str = [NSString stringWithFormat:#"2013-04-23T18:22:00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"US/Eastern"]];
NSDate *dte = [dateFormat dateFromString:str];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal setTimeZone:[NSTimeZone timeZoneWithName:#"US/Eastern"]];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = dte;
notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"trumpet.m4a";
notifyAlarm.alertBody = #"Message";
[app scheduleLocalNotification:notifyAlarm];
}
}
}
Try the following code:
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:30];
[dateComps setMonth:4];
[dateComps setYear:2013];
[dateComps setHour:23];
[dateComps setMinute:0];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
localNotif.alertBody = #"Message";
localNotif.repeatInterval = 0;
localNotif.soundName = #"trumpet.m4a";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
One thing I notice is that I doubt you meant to use YYYY in your date formatter. From Apple Docs
A common mistake is to use YYYY. yyyy specifies the calendar year whereas
YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.
In most cases, yyyy and YYYY yield the same number, however they may
be different. Typically you should use the calendar year.
When I changed this your code worked for me and posted a notification.

UILocalNotification is not working in my device

Here is the code that I have used in my application to generate local notification:
UILocalNotification *aNotification = [[UILocalNotification alloc] init];
if (aNotification == nil)
{
NSLog(#"localNotif");
return;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *sourceDate = [dateFormatter dateFromString:#"2013-02-16 15:00:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
aNotification.fireDate = destinationDate;
aNotification.timeZone = [NSTimeZone defaultTimeZone];
NSLog(#"destinationDate %#",destinationDate);
aNotification.alertBody = #"About To Start";
aNotification.alertAction = #"View";
[[UIApplication sharedApplication] scheduleLocalNotification:aNotification];
[dateFormatter release];
[destinationDate release];
[aNotification release];
But when I test it in device by changing the date and time from Settings application, the event is not firing. What am I doing wrong?
When I print destinationDate in console it is coming like this:
destinationDate 2013-02-16 15:00:00 +0000