Notify in specific date and time - iphone

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;

Related

Is it possible to have two local notification in app

I wanted to have two local notification ,both have different time ,let say my first notification will give alert after 1 minute and the second will give alert after 2 minutes.
I have tried it to create two in appDelegate but only first one is giving me the notification and not the second one .
How can I achieve this ?
Yes It is Possible to set two LocalNotification in any iOS Application
See below method by which you can set multiple LocalNotifications
You just Need to pass required parameter to this method.
- (void)setAlarmFor:(NSArray*)datesArray forTime:(NSString*)atTime notificationName:(NSString*)name
{
for(int dayIndex=0;dayIndex <[datesArray count];dayIndex++)
{
Class cls = NSClassFromString(#"UILocalNotification");//
if (cls != nil) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
NSString* dateStr=[datesArray objectAtIndex:dayIndex];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *tempDate = [dateFormatter dateFromString:dateStr];
NSString *tempString = [dateFormatter stringFromDate:tempDate];
tempString = [NSString stringWithFormat:#"%# %#",tempString,atTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate *firetAtThisDate = [dateFormatter dateFromString:tempString];
UILocalNotification *localNotif = [[cls alloc] init];
localNotif.fireDate =firetAtThisDate;//here set the Date at which mnotification fire;
NSLog(#"Notification date is:%#",firetAtThisDate);
localNotif.alertBody =name;
localNotif.alertAction = #"Your'Alert message";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:tempString
forKey:tempString];//by using this we can further cancel the Notification
localNotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[dateFormatter release];
}
}
}
And In Appdelegate Class Prepare Action what you want as Notification Fire
//This Below Line will goes to the Appdelegate DidFinishLaunching Method
Class cls = NSClassFromString(#"UILocalNotification");
if (cls)
{
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
//do what you want
}
}
application.applicationIconBadgeNumber = 0;
//End of Appdelegate DidFinishLaunching Method.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
//do what you want
}
sure , you use one or more local notifications in your app. try this code in your project
-(void) setLocalNotification
{
NSTimeInterval todayTimeIntervel=[[NSDate date]timeIntervalSince1970];
NSTimeInterval nextOneMinTimeIntervel;
nextOneMinTimeIntervel = todayTimeIntervel + 60 ;
NSTimeInterval nexttwoMinTimeIntervel;
nexttwoMinTimeIntervel = todayTimeIntervel + 60*3;
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:nextOneMinTimeIntervel];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:nexttwoMinTimeIntervel];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSString *strDate1 = [dateFormat stringFromDate:date1];
NSString *strDate2 = [dateFormat stringFromDate:date2];
NSArray *arr = [NSArray arrayWithObjects:strDate1,strDate2, nil];
NSArray *titleArr = [NSArray arrayWithObjects:#"First LocalNotification",#"Second LocalNotification", nil];
for (int i =0; i < 2; i++)
{
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[arr objectAtIndex:i],#"dateStr",[titleArr objectAtIndex:i],#"title", nil];
[self scheduleLocalNotification:dic];
}
}
-(void) scheduleLocalNotification:(NSMutableDictionary*) dic
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSLog(#"%#", [dateFormat dateFromString:[dic objectForKey:#"dateStr"]]);
localNotif.fireDate = [dateFormat dateFromString:[dic objectForKey:#"dateStr"]];
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertAction = #"View";
localNotif.alertBody = [dic objectForKey:#"title"];
localNotif.userInfo = dic;
NSLog(#"value of infoDic %#",dic);
localNotif.repeatInterval = NSDayCalendarUnit;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
// get app instance
UIApplication *app = [UIApplication sharedApplication];
// create local notif
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
NSDate *oneMinuteFromNow = [[NSDate date] dateByAddingTimeInterval:60];
notification.fireDate = oneMinuteFromNow;
notification.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage = #"First";
notification.alertBody = notificationMessage;
notification.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification];
// fire notification right away
[app presentLocalNotificationNow:notification];
}
UILocalNotification *notification1 = [[[UILocalNotification alloc] init] autorelease];
if (notification1) {
NSDate *oneMinuteFromNow1 = [[NSDate date] dateByAddingTimeInterval:120];
notification1.fireDate = oneMinuteFromNow1;
notification1.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage1 = #"Second";
notification1.alertBody = notificationMessage1;
notification1.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification1];
// fire notification right away
[app presentLocalNotificationNow:notification1];
}
By writing this you ll get one notification after 1min and second one after 2 min. In "didReceiveLocalNotification" method you can check the notification type and can show alert msg.
Hope this will help you.
Yes you can use the multiple local notification in your application.
Check this link.
Hope it helpfull for you

NSNotification before 2 days from targeted date

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.

scheduleLocalNotification doesn´t work

when I get the time/date from dateTimePicker.date, the Notification work, but when I fire the date with dateFromString, the notification doesn´t work.
The Code:
-(IBAction)alarmSetButtonTapped:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale systemLocale];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *tmp = #"2012-09-05 10:19";
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFromString = [dateFormatter dateFromString: tmp];
NSLog(#"Alarm TMP %#", dateFromString);
[dateFormatter release];
[self scheduleLocalNotificationWithDate:dateFromString];
[self presentMessage:#"Alarm set"];
};
Please can you help me??
EDIT:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up";
notification.soundName = #"clock.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
}
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:yourDate];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber=1;
// Setup alert notification
[localNotification setAlertAction:#"Open App"];
[localNotification setAlertBody:[randonQuotesDic objectForKey:#"Rajneesh"]];
[localNotification setAlertBody:#"You had set a Local Notification on this time"];
localNotification.soundName=UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];

how to add date components to NSDate having time format

please see the following code
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
NSDate *dat=pickerView.date;
NSString *timerString=[outputFormatter stringFromDate:dat];
[startTimer setTitle:timerString forState:UIControlStateNormal];
timeString=timerString;
[self updatePrayerTimigs];
//dat=[NSDate dateWithTimeIntervalSinceNow:20];
UILocalNotification *local=[[[UILocalNotification alloc]init] autorelease];
UIApplication *app=[UIApplication sharedApplication];
if(local){
local.fireDate=dat;
local.timeZone=[NSTimeZone defaultTimeZone];
//local.repeatInterval=0;
local.alertBody=#"Hello";
local.repeatInterval=NSMinuteCalendarUnit;
[app scheduleLocalNotification:local];
[app presentLocalNotificationNow:local];
}// end of the if
I am selecting only time (Hour , minute) from UIDatePicker so i can fire notifications, so I decided to add current date to above NSDate with selected from UIDatePicker , please help how can I do that
Try this to format the date
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];

NSDate 24h format: delete first '0' from hours

When I want a date in 24 hour format, I see the hours such as 05:00. How can I delete the first 0 to get 5:00? I tried this:
NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setTimeStyle:NSDateFormatterShortStyle];
mystring = [fhours stringFromDate:newDate];
[fhours release];
Like this
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm"];
NSDate *now = [NSDate date];
NSString *theTime = [timeFormat stringFromDate:now];
At line
[timeFormat setDateFormat:#"HH:mm"];
You can erase one H like this
[timeFormat setDateFormat:#"H:mm"];
You should get the right result with the following:
NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setDateFormat:#"H:mm"];
mystring = [fhours stringFromDate:newDate];
[fhours release];