Notification delivering at 12 AM instead of 7 AM - ios

I am trying to schedule a notification to be delivered at 7 AM. But it gets delivered at 12 AM. Please help me what I am doing wrong. This is my code.
NSString *date = #"4/14/2013 07:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm"];
NSDate *thisDate = [df dateFromString:date];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = thisDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"This is notification";
localNotification.alertAction = #"view";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = Nil;
self.badgeCount ++;
localNotification.applicationIconBadgeNumber = self.badgeCount;
localNotification.userInfo = Nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I expect this notification to be delivered at 7 AM. Instead it pops up at 12 AM. What is going wrong here ?

I bet localNotification.timeZone and df.timeZone are not the same...
Add this to your DateFormatter:
df.timeZone = [NSTimeZone defaultTimeZone];

Related

Showing notifications while app in background

I need to show certain local notifications while app is in the background. How can I do it, without the help of NSNotificationCenter?
Andrey Chernukha in first comment is right.
Here is the code to implement simple Local Notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = 0;
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:10];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Test local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

UILocalNotification is not firing on correct time

I am trying to set a UILocalNotification but it is not firing on time. It fires 2 hours later then it should. Here how I calculate the alert date:
NSDate *etkinlikDate = [gregorian dateFromComponents: components];
sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
destinationTimeZone = [NSTimeZone systemTimeZone];
sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:etkinlikDate];
destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:etkinlikDate];
interval = destinationGMTOffset - sourceGMTOffset;
etkinlikDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:etkinlikDate];
here etkinlikDate returns me the correct value. (The date I want to fire the notification)
and here how I set the alert:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = etkinlikDate;
localNotification.alertBody = [NSString stringWithFormat: #"%# %#", [sClass className], etkinlikSaati];
localNotification.soundName = #"receivedmessage.caf";
localNotification.alertAction = #"Show me the item %d";
localNotification.timeZone = [NSTimeZone systemTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
Although the etkinlikDate is correctly set the local notification is not firing on time.
Update: Lets say I want to set the alert on 16:30.
etkinlikDate returns 16:30 but alert fires on 18:30.

UILocalNotification did not show up when I changed date and time on device in ios8

I scheduled the Local notification for current day +1 and time say 10AM.When I change the device date to current day+1 and time 10PM then notification didn't show up.
Code is given below:
UILocalNotification * localNot = [[UILocalNotification alloc] init];
localNot.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
#"ReminderId", #"REM01", nil];
localNot.alertBody = #"Alarm";
NSString *str =#"10/15/2014 9:15 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.timeZone = [NSTimeZone defaultTimeZone];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *date = [formatter dateFromString:str];
localNot.timeZone = [NSTimeZone defaultTimeZone];
localNot.fireDate = date;
localNot.repeatInterval = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNot];
Need input.Thanks in advance.

Local Notification set fire time

I want to set the daily alarm on the bases of user input. Like user will select time from date picker "10:30", Then i need to set alarm at a that time daily. I write the following code:
func setAlarmAtTime(#time:NSString, withMessage message:NSString){
var loacalNotification = UILocalNotification();
var calendar = NSCalendar.currentCalendar();
calendar.timeZone = NSTimeZone.localTimeZone()
var components = calendar.components(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: NSDate.date());
NSLog("%#",NSDate.date())
NSLog(time);
var timeComponents = time.componentsSeparatedByString(":");
components.hour = timeComponents[0].integerValue;
components.minute = timeComponents[1].integerValue;
if components.isValidDateInCalendar(calendar){
var fireDate = calendar.dateFromComponents(components);
NSLog("%#",fireDate!);
loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
loacalNotification.timeZone = NSTimeZone.localTimeZone();
loacalNotification.fireDate = fireDate;
loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
loacalNotification.soundName = UILocalNotificationDefaultSoundName;
loacalNotification.alertBody = message;
}
But it shows different time based on time zone following are the behaviour when i try to set alarm at 6:40:
Current Date: 2014-08-12 12:07:21 +0000
Alarm Time: 6:40
Fire Date: 2014-08-12 01:10:00 +0000
I tried to set time zone to local as well as current but nothing works :(
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *reminderDate = [df dateFromString:self.lblDate.text];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = reminderDate;
localNotification.alertBody = self.txtName.text;
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName =#"sound.mp3";
NSMutableDictionary *notifyDict = [[NSMutableDictionary alloc] init];
[notifyDict setValue:self.lblType.text forKey:NOTIFICATION_TYPE];
[notifyDict setValue:self.txtName.text forKey:NOTIFICATION_TITLE];
if (![self.tvDescription.text isEqualToString:#"Write Description"]) {
[notifyDict setValue:self.tvDescription.text forKey:NOTIFICATION_DESCRIPTION];
}
[notifyDict setValue:self.lblDate.text forKey:NOTIFICATION_DATE];
localNotification.userInfo = notifyDict;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
The time that it is given, is in greenwich mean time (GMT)
we just need to set
localNotification.timeZone = [NSTimeZone systemTimeZone];
as it will represent your device or system time zone and schedule your app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Invoking UILocalNotification based on Text/Value of Label

My question is :
i have a TableVC wherein i have CustomCells.
These customCells have Various Label and one of them is Time label.
The text of the timeLabel would be fetched by Parsing an xml.
But as of now assuming that i used static content for e.g.: 2:45pm.
Is it possible for me to give this text as fireDate of UILocalNotification and off-course it should fire on the said time.
It should be done on click of cell that is in "didSelectRowAtIndexPath".
Or this cannot be done ??
And yes if this is a repetition question,you did not like it, or if if there is some spelling mistake or anything related Please do not mark it with down votes.
I have seen in the past where people play such cheap gimmicks and use their so called reputations in an unhealthy and unprofessional way.
Your related answer's are warmly welcomed.
Thank You !!!
EDIT
i tried this thing
NSString *str = cell.timeLabel.text;
NSLog(#"str = %#",str);
formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
date = [formatter dateFromString:str];
the above in cellForRowAtIndexPath
and
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:5 sinceDate:date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"You are notified";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
}
NSDate *currentDate=[NSDate date];
NSString *dateStr=#"7:10 PM";
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *preFix=[dateFormatter stringFromDate:currentDate];
NSString *datetoFire=[NSString stringWithFormat:#"%# %#",preFix,dateStr];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate *fireDate = [dateFormatter dateFromString:datetoFire];
fireDate=[fireDate dateByAddingTimeInterval:-(60*60*(5.5))];
NSLog(#"date is %#",fireDate);
// Cancel the previously scheduled notification, if any.
if (localNotification == nil) {
return;
}
localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Good Morning Buddies";
localNotification.alertAction = #"View";
localNotification.fireDate = fireDate;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Resources