How to provide the Notification in iphone.(GPS Notification) - iphone

I am building an application where i show the bus stops on the map and the user current location as well.I want to implement the notification that when the user is say x meters from one bus stop ,there should be a notification like alert,sound .So that he can prepare for his stop.There may be many stops on the map and i want to do it for everystop.
Please help me how to implement that.Whether it would be LocalNotification,Then how to use this in this way..........
Thanks in advance

Do all your distance calculations in didUpdateToLocation.
You can send local notifications in iOS 4, using UILocalNotification class.
Here is some sample code:
UILocalNotification *noti = [[UILocalNotification alloc] init];
noti.fireDate = someDate;
noti.alertBody = someText;
noti.alertAction = nil;
noti.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
[noti release];
Make sure the date is as close as possible as [NSDate date], so it will appear right away.

Related

Set repeatInterval in local notification

I want to set repeat interval to the value which user selects from date picker.I have date picker of type countdown mode in my application.If user selects 4 hours 15 minutes from date picker then I am setting firedate using the following code and alarm rings.
[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]]
But I want that notification should repeat after every 4 hours 15 minutes until user cancels it. I have done r&d searched a lot but I can not figure out.The code I have used so far is:
localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]]];
if(localNotification.fireDate){
[self _showAlert:#"Time is scheduled" withTitle:#"Daily Achiever"];
}
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertBody=#"alaram";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setAlertAction:#"View"];
[localNotification setRepeatInterval:[pickerTimer countDownDuration]];
//The button's text that launches the application and is shown in the alert
// [localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text
//[localNotification setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatInterval=NSHourCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
//[alertNotification setHidden:NO]; //Set the alertNotification to be shown showing the user that the application has registered the local notification
Please help me to solve. Thanks a lot in advance.
I have solved the problem using following code:
-(void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
testDate=notif.fireDate;
NSLog(#"Recieved Notification %#",notif);
[self playSoundWithNotification:notif];
[self _showAlert:#"Alaram" withTitle:#"Daily Achiever"];
}
-(void)_showAlert:(NSString*)pushmessage withTitle:(NSString*)title
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
if (alertView) {
FocusViewController *fvc=[[FocusViewController alloc]initWithNibName:#"FocusViewController" bundle:nil];
[fvc insert:[NSDate dateWithTimeInterval:refTimeIntrval sinceDate:testDate]];
}}
Here testDate and refTimeInterval are variables declared in .pch file.
We cannot set custom Value for repeatInterval in the UILocalNotification.
The simplest way you can achieve this by creating a new localNotification every 4.25 hours and copy the notification details of the the the existing one and delete it while handling the local notification.
Other way is to change the firedate while handling the local notification.

Turn an alarm notification on and off with buttons

I am creating a clock app with a single alarm.
I have a button to set the alarm, a button to turn the alarm on, and a button to turn the alarm off.
The set button works using a local notification:
UILocalNotification *scheduledAlert;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
scheduledAlert = [[UILocalNotification alloc] init];
scheduledAlert.applicationIconBadgeNumber=1;
scheduledAlert.fireDate = alarmPicker.date;
scheduledAlert.timeZone = [NSTimeZone defaultTimeZone];
scheduledAlert.repeatInterval = NSDayCalendarUnit;
scheduledAlert.soundName=#"alarm_clock_ringing.wav";
scheduledAlert.alertBody = #"I’d like to get your attention again!";
[[UIApplication sharedApplication]
scheduleLocalNotification:scheduledAlert];
Everything works just fine.
However, I do not know how to code the On and Off buttons.
For the Off button you have to use something like CancelAllCurrentNotifications.

Unique entries in Notification Center and 'repeatInterval'

I'm issuing a local notification alert (iOS 5) that will repeat its reminder periodically (every minute) until the user acknowledges it by setting repeatInterval on the UILocalNotification instance added to [[UIApplication sharedApplication] scheduleLocalNotification:
The effect of setting 'repeatInterval' is that each time the notification is repeated while not acknowledging it, there's another entry in the Notification Center. e.g: After 5 minutes of not ack'ing the firing, there are 5 separate entries in the Notification Center.
Is there way to only have one entry, but sound/vibrate alert every 1 minute?
Here's how I'm creating the local notification:
UILocalNotification *localNotify = [[UILocalNotification alloc] init];
localNotify.fireDate = aFireDate;
localNotify.timeZone = [NSTimeZone defaultTimeZone];
localNotify.userInfo = userInfo;
// 3. Configure the substance of the notification: alert, icon badge number, and sound.
localNotify.alertBody = NSLocalizedString(alertTitleText, nil);
localNotify.alertAction = NSLocalizedString(alertActionText, nil);
localNotify.soundName = UILocalNotificationDefaultSoundName;
localNotify.applicationIconBadgeNumber = 1;
localNotify.repeatInterval = NSMinuteCalendarUnit;
// Schedule the local notification for delivery.
[[UIApplication sharedApplication] scheduleLocalNotification:localNotify];
Unfortunately no. As long as the user has Notification Center turned on for your app, every local notification you fire will show up as a distinct entry in Notification Center; even if they are multiple firings of the same UILocalNotification object. The UILocalNotification API gives you no control over this.

Creating a Time Based Reminder App in iPhone

I am working on time based reminder App. in which the user enter his reminder and time for the reminder. The problem is that how to continuously comparing the current time with the user defined time. Any sample code will greatly help. because i am stuck on this point.
Comparing the current time vs. the user defined one is not the right design pattern.
UIKit offers the NSLocalNotification object that is a more high-level abstraction for your task.
Below is a snip of code that create and schedule a local notification at the choosen time:
UILocalNotification *aNotification = [[UILocalNotification alloc] init];
aNotification.fireDate = [NSDate date];
aNotification.timeZone = [NSTimeZone defaultTimeZone];
aNotification.alertBody = #"Notification triggered";
aNotification.alertAction = #"Details";
/* if you wish to pass additional parameters and arguments, you can fill an info dictionary and set it as userInfo property */
//NSDictionary *infoDict = //fill it with a reference to an istance of NSDictionary;
//aNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:aNotification];
[aNotification release];
Also, be sure to setup your AppDelegate to respond to local notifications, both at startup and during the normal runtime of the app (if you want to be notified even the application is in foreground):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *aNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
if (aNotification) {
//if we're here, than we have a local notification. Add the code to display it to the user
}
//...
//your applicationDidFinishLaunchingWithOptions code goes here
//...
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//if we're here, than we have a local notification. Add the code to display it to the user
}
More details at the Apple Developer Documentation.
Why not use NSLocalNotification which you can set for a specified time, much like a calendar event. Alternatively you can add calendar events to the users calendar with EKEventKit
Tutorial for local notifications.
Tutorial for event kit.

Clear app badge with local notifications

I'm trying to clear my app's "unread" badge with a UILocalNotification. Logically you would think this would be done by setting applicationIconBadgeNumber of a UILocalNotification instance to 0. But it doesn't work, and the docs for applicationIconBadgeNumber say "The default value is 0, which means "no change.”"
So is there really no way to clear a badge with local notifications once it's been set?
Update: Some simple code:
-(void)applicationDidFinishLaunching
{
// Set the appication icon badge to 1 in 10 minutes, using a local notification so it works in the background:
// This works fine.
UILocalNotification *episodeNotification = [[UILocalNotification alloc] init];
episodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 10)];
episodeNotification.timeZone = [NSTimeZone defaultTimeZone];
episodeNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:episodeNotification];
[episodeNotification release];
// Clear the application icon badge in 20 minutes, again using a local notifcation so it works in the background:
// This doesn't work. According to the docs for local notification it's not supposed to
// because (applicationIconBadgeNumber = 0) means "Do not change the badge"
// I'm looking for an alternative if it exists.
UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init];
clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 20)];
clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone];
clearEpisodeNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification];
[clearEpisodeNotification release];
}
I had the same problem. When setting the badge from a local notification, setting it to 0 is the default for 'no change', while doing it straight from the application would clear it. Setting it to a negative number through a local notification solved the problem.
try:
clearEpisodeNotification.applicationIconBadgeNumber = -1;
Yes, it is possible to clear the badge from the app itself.
I use the code below in one of my apps, and it works as expected (i.e. clears the badge):
//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;