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.
Related
ViewController:
- (void)viewDidLoad
{
`[super viewDidLoad];`
dateFormatter.dateFormat = #"dd/MM";
timeFormatter.dateFormat = #"HH:mm";
NSString *dateString = [dateFormatter stringFromDate:dateTime];
NSString *timeString = [timeFormatter stringFromDate:dateTime];
if ([dateString isEqual:#"23/06"]) {
if ([timeString isEqual:#"23:30"]) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}
AppDelegate:
[[UIApplication sharedApplication] scheduledLocalNotifications];
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
}
The notification isn't receiving when time equals string and date.
Please help!
The core issue with this (aside from using strings to compare dates) is that viewDidLoad gets called only once in this application. Because you are only scheduling the local notification when the current date is 11:30, it will never get called, since you also set the fireDate to the same exact time.
The thing is, local notifications get scheduled prior to the event. In fact, you should set dateTime to the desired schedule time and then set the local notification.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2016];
[dateComponents setMonth:6];
[dateComponents setDay:23];
[dateComponents setHour:23];
[dateComponents setMinute:30];
dateTime = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Based on your uploaded code, it looks like you are going to be grabbing the date from the datePicker, so eventually, you will move the UILocalNotification portion to the button method and use [datePicker date] as the fireDate. Meaning, you can ignore the dateComponents part.
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];
I want to know if it is possible to add seconds to a local notification? I am trying to create a loop to schedule local notifications 30 seconds after each others. So, in the loop below, can I keep on "delaying" the firedate 30 seconds after each other. I don't know if that question makes sense, but that's the best I can describe my problem as. Think of it as a 30 second interval, but manually scheduling each notification.
for (notifs = 1, notifs <= 64, notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [self.timePicker date];
//can i write it like [self.timePicker date] + 30000?
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
Thanks.
You could use NSDate's dateByAddingTimeInterval: and just pass the current iteration number multiplied by 30.
for (notifs = 1; notifs <= 64; notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[self.timePicker date] dateByAddingTimeInterval:notifs * 30.0];
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
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];
I am trying to schedule a local notificaition that will repeat after every 1 sec once the notification is fired. The notification is fired 10 sec after the application starts.
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Did you forget something?";
notif.alertAction = #"Show me";
//notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = #"applause-light-01.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSSecondCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Even thought I have used notif.repeatInterval = NSSecondCalendarUnit, notification repeat after 60 sec. What is that I am doing wrong?
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
This line of code makes your local notification to fire after the new date is created. If you want the notifications right away then you should just create a simple date like this,
notif.fireDate = [NSDate date];
Hope this helps.