scheduleLocalNotification doesn´t work - iphone

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];

Related

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;

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

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

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.