iOS: Local Notifications Don't Fire On Time - iphone

The issue is that my notifications aren't going through when they are set up to. I put a date picker in the nib and a button underneath it. The user is supposed to set the date in the picker, and clicking the button set up the notification to fire 60 hours before the date. So, it looks like I'm just having issues with getting the firedate to recognize the date in the date picker. Here is the code, it is in my view controller:
- (IBAction)scheduleNotifButton:(id)sender {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *selectedDate = [self.datePicker date];
localNotif.fireDate = [selectedDate dateByAddingTimeInterval:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Event scheduled."
message:#"You will be notified three days before the event."
delegate:nil
cancelButtonTitle:#"Okay."
otherButtonTitles:nil];
[alert show];
}
And here is some additional code if that helps, this is more code from my view controller that deals with saving the date in the picker that the user entered:
- (IBAction)dateChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:#"ImportantDatesViewController.selectedDate"];
[defaults synchronize];
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"ImportantDatesViewController.selectedDate"];
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.datePicker setDate:storedDate animated:NO];
}
I've been digging through this code for 3 days now and cannot figure out why my notifications aren't going through when they are supposed to. Any help is very much appreciated, thank you!

Do you understand that UILocalNotifications and remote notifications are different things?
In didRegisterForRemoteNotificationsWithDeviceToken you get the device token to be used for remote notifications. Remote notifications are sent from a server so you have to save that token on your server and then use your server to send any remote notifications.
Why don't you change this:
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60];
to this:
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
Then the local notification should go off in a minute. Maybe the problem is the date that you are setting.

How do you know your notifications aren't working?
Local and push notifications are different. If your app is currently active, a local notification won't actually show an alert message. It will just call a UIApplicationDelegateMethod
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Related

How to fire action on date change?

I have SQLite database.
I want to insert row in database when date changes.
I just know that i should use UILocalNotification,But I don't know how.
Other way is to run NSTimer in background but I don't want to do that.
From tutorial I tried:
UIApplication* app = [UIApplication sharedApplication];
NSDate *date = [NSDate date];
// create the notification and then set it's parameters
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = #"DONE:";
// this will schedule the notification to fire at the fire date
[app scheduleLocalNotification:notification];
// this will fire the notification right away, it will still also fire at the date we set
[app presentLocalNotificationNow:notification];
}
But in my app it should insert in database either app is in background or foreground. Insertion should place when date changes.
If you want to go with UILocalNotification then see below information.
Set fireDate of UILocalNotification as you need and your notification will generated on this date.
And you will get generated notification at 2 (Two) ways. such like,
1) in didFinishLaunchingWithOptions
=> it is helpful if your application is not running;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
/// do your stuff
}
.
.
}
2) use delegate method of UILocalNotification
=> it is helpful if your application is running;
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
/// do your stuff
}
You can write your insert data here;
Add this code in didFinishLaunchingWithOptions method
// date change
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dateChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
Implement method in YourDelegate.m file
-(void)dateChange
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"LastAppOpenTime"] != nil)
{
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"LastAppOpenTime"];
NSDate *currentDate = [NSDate date];
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:lastDate];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
if (hoursBetweenDates >= 24)
{
//update database here.
}
}
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"LastAppOpenTime"];//Store current date
}
You can store [NSDate date]; to some variable and put condition according to last date and change that variable inside that condition with today's date with the use of [NSDate date]; and do modifications as you want.

How change the sound name of local notification?

I have an application in which i have button. When i click on button then generate a local notification. The variable of local notification is set in appDelegate file. For generate local notification i used this code:-
UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.alertBody = #"Time for another cup of coffee!";
ln.applicationIconBadgeNumber = 1;
ln.fireDate = notification_date; //[NSDate dateWithTimeIntervalSinceNow:15];
ln.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSString *string_date=[formatter stringFromDate:notification_date];
NSDateFormatter* formatter_alarm = [[[NSDateFormatter alloc] init] autorelease];
formatter_alarm.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter_alarm setDateFormat:#"hh:mm a"];
NSString *str=[formatter_alarm stringFromDate:notification_date];
appDelegate.alarm_time=[NSString stringWithFormat:#"%#",str];
NSLog(#"%#",appDelegate.alarm_time);
[[NSUserDefaults standardUserDefaults] setObject:appDelegate.alarm_time forKey:#"alarm_on_time"];
[[NSUserDefaults standardUserDefaults] setObject:string_date forKey:#"alarm_on_date"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"alarm will activate on%#",notification_date);
ln.soundName = #"alarm.wav";
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
ln.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
// if(appDelegate.appDelegate_notification ==nil)
// appDelegate.appDelegate_notification= [[UILocalNotification alloc] init];
appDelegate.appDelegate_notification=ln;
[ln release];
Now i have a another button which is used for change the sound of local notification. I wan t that when user click on that button then change the sound of local notification. For that purpose i use this code:-
appDelegate.appDelegate_notification.soundName = #"Blow.wav";
Now problem is that when i click on another button then sound of local notification is not changed. How make that event on button click?
Thanks in advances...
When we generate any local notification then we can set sound file at that time . If we want to set sound after setting notification then i get only one way that i share here. When we edit any sound of alert then first we have to save fire date of alert and after that we will cancel that alert and set a new alert with same fire date anad this time we will pass new sound file. I use this and working fine.

How to initialize local notification?

I want to implement local notification in my clock app.Basically i want that a music file should be played after every half an hour like in ship's clock in which chimes are played after every 30 minutes.
Can anyone give rough idea as how i can implement this functionality even when the app enters in background?
I recently used the Local notification stuff and used the following functions
//Setting up the Local Notifications
for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly
NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,#"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];
[self scheduleNotificationWithItem:dataDict];
}
Where scheduleNotificationWithItem is defined as
- (void)scheduleNotificationWithItem:(NSDictionary*)item {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) return;
localNotification.fireDate = [item valueForKey:FIRE_TIME_KEY];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%#", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = item;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
Finally you can handle these notifications as
You can handle these notifications as follows
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary
}
reading the developer documentation will help you more to understand the stuff.Hope it helps
You can use UILocalNotifications and set their 'firedate', according to your requirement and then schedule the notification. These notifications doesn't bother whether your app is running or is in background they will always show up like an alertview.

how to set uilocalnotification firedate for a fixed date?

i want to set localnotification say for a program which starts at 6:00 pm.For that i have taken the time in a date variable and i am comparing it with current date from system.
Say setDate is for fixed date i.e 6.00 pm so i have to set firedate such that it shows the notification before 30 mintes the program starts. The examples i have seen in that the firedate is set according to currentdate.
Can someone tell me how can i set firedate according to my fixed date??
You fire the local notification this way
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate date];// Now here you can manage the fire time.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"BusBuddy";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"You are near to reach the Bus Stop" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
This line of code will work for you. you just need to provide the date time for this
localNotif.fireDate = [NSDate date];
And now for formatting your date time you can refer to these links
iphonedevelopertips.com
developer.apple.com, CFDatesAndTimes
developer.apple.com, DataFormatting
Well then you can handle your local notification in application delegate file when ever you get the notification.
e.g. here is the delegate method which is fired everytime when you get the local notification.
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
//Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hey Neha" message:#"Sanjay wants to be your friend " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
SystemSoundID bell;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WhoopFlp" ofType:#"wav"]], &bell);
AudioServicesPlaySystemSound (bell);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
i=0;
}
}
So what I am doing here is simply showing the alert and playing a system sound when my local notification occurs.
So now what you want is to navigate to the page in the program where you were when you get the local notification.So simply in this delegate method you need to allocate your view controller and need to push the view controller to that view where you want to be.
That would solve your problem.
Well i had a similar kind of problem what i wanted is to show the notification in the background and in the front end as well, so writing the 2 different methods in my app was not worthful.so i handled it this way in the delegate method which will show the notification in the front end as well.
Good luck to you.
localnotification =[[UILocalNotification alloc]init];
[localnotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[lodatepicker countDownDuration]]];
[localnotification setAlertAction:#"Launch"];
[localnotification setHasAction: YES];
[localnotification setAlertBody:[lotextview text]];
// [localnotification setSoundName:musicString];
localnotification.timeZone = [NSTimeZone defaultTimeZone];
[localnotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1];
[[UIApplication sharedApplication] scheduleLocalNotification:localnotification];

local notification "didReceiveLocalNotification" calls twice

I am handling local notifications using:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
And to schedule a local notification:
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *fireDate = [NSDate date];
localNotif.fireDate = [fireDate dateByAddingTimeInterval:minutesBefore*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = kCFCalendarUnitMinute;
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"LocalEvent notification in %i minutes.", nil),minutesBefore];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"This is dict, you can pass info for your notification",#"info",nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
NSLog(#"Event scheduled");
}
When I receive a notification, didReceiveLocalNotification: is called twice.
Am I doing something wrong?
Please help.
Thanks.
I think there is a known bug in the simulator, that fires the delegate notification method twice. It should not happen on the device, tethered to XCode or not.
i was also facing the same problem and the solution which i find is that write this code in didReceiveLocalNotification
if (state == UIApplicationStateActive) {
NSLog(#"UIApplicationStateActive");
}
else if(state == UIApplicationStateInactive){
NSLog(#"UIApplicationStateInActive");
}
here in these condition i just write the code which i want my application to do on notification , in Active mode and in inactive mode
I suspect that the notification is being retriggered as long as its in the same second still. I fixed it by setting the fireDate to nil in the handler:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
notification.fireDate = nil;
}
I had the same issued. It was caused by calling 'registerUserNotificationSettings' twice in the AppDelegate's 'didFinishLaunchingWithOptions.' However, simply removing the duplicate call did not fix the problem yet. I had to delete the app and then rebuild. Only then did the double local notification issue get fixed.