Handle Multiple Local Notification in IOS SDK - iphone

In my Background method, i scheduled the two notification as follow.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification setAlertAction:#"Launch"]; //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]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
// ** Notification 2***
localNotification2 = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h
[localNotification2 setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification2 setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification2 setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text
[localNotification2 setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification2 setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; //Schedule the notification with the system
}
It works fine for coming the notification.
Question
How can I detect which notification is come in didreceivenotification method??
Because I want to do the different task based on the notification came.

You can set the userInfo Dictionary
localNotification1 = [[UILocalNotification alloc] init];
localNotification1.userInfo = #{ "type" : #1 };
...
localNotification2 = [[UILocalNotification alloc] init];
localNotification2.userInfo = #{ "type" : #2 };
...
...

you can set a NSDictionary in userInfo property of a UILocalNotification
So you can do it this way...
localNotification1.userInfo = [NSDictionary dictionaryWithObject:#"1" forKey:#"NO"];
localNotification1.userInfo = [NSDictionary dictionaryWithObject:#"2" forKey:#"NO"];
and compare the key "NO" in didReceiveNotification.

Related

iOS - UILocalNotification does not fire (when set in background)

I have this code to schedule a UILocalNotification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = #{PLANNED_EVENT_ID : #"some id"};
localNotification.fireDate = someStartDate;
localNotification.alertBody = #"Some text";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This method works as expected when I set the above when application is in foreground, but if I use this code when application is in background (pressed home button), then it is not fired at all?
Why on earth will this not work? How can I get the result I need? I need to able to use this code when application is in background.
Thanks!
To be more clear, in my AppDelegate i have the method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)
When I get the first notification inside this method (which works, it is scheduled in a view controller), I schedule another notification in the same way (1 min from now). So I tried to have the application in foreground, then it worked, but when I had it in background, it didn't.
Tested on Simulator though.
The method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)
is called when the app opens after a local notification has been shown to the user and the user has opened the app from the notification. If the user doesn't open the app from the notification then this method will not be called. So your next notification is never actually added.

Notification is being disappeared from statusbar in iphone

I have implemented notification in my app. When notification gets fired, alert is disappeared without pressing Ok or cancel button from alert view of notification and it also being disappeared from status bar. This is happening while application firing notification is either in background or in foreground. what would be the reason behind it?
I want to show notification alert till user is pressed OK or Cancel button, how could I achieve it?
Do I need to have some setting in notification setting or right some code for that?
My Code is as given below..
NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setDateFormat:#"dd/MM/yyyy / HH:mm:ss"];
NSDate *toDate = [dateFormatter3 dateFromString:timeBut.titleLabel.text];
[dateFormatter3 release];
NSLog(#"toDate=%#",toDate);
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object
[localNotification setFireDate:toDate]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:[remTextField text]]; //Set the message in the notification from the textField's text
[localNotification setHasAction: YES]; //Set that pushing the button will launch the
[localNotification setSoundName:UILocalNotificationDefaultSoundName];
//[localNotification setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
[localNotification release];
successalert=[[UIAlertView alloc] initWithTitle:#"DemoTable" message:#"Simple Reminder is successfully added" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[successalert show];
[successalert release];
I have implemented below method in appdelegate.
//Getting notification while running
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"DemoTable" message:notif.alertBody delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
// delete the notification from the system
[application cancelLocalNotification:notif] ;
}
Thanks..
The default notification style in iOS6 is show the notification on the bar. In this case an alert will show for a few seconds and disappear, notification remains in the notification bar until user open it. This is the default behavior of the notification center. You cannot change that. The user of your app has to decide the style of the alert wether he needs old style alert notification or notification on the notification center. If he choose old style alert from the settings of the iPhone, then only you can prompt the user to open the app through the alert button action. Otherwise app will open when the user selects the notification from the notification bar. This is user preference you cannot do anything with that

Alarm's notification only trigger in background

I am creating an alarm application ,in that i have created notification that display message and play sound. But now its work only in background,i have to minimize my application to trigger my notification,if my app is open then notification does not show.
code for notification are:
- (void)viewDidLoad
{
[super viewDidLoad];
dateTimePiker.date = [NSDate date];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = #"Wake up";
notification.soundName = #"ma.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)SetAlarmBtnTapped:(id)sender {
.....
.....
[self scheduleLocalNotificationWithDate:dateTimePiker.date];
}
implement application:didReceiveLocalNotification: on your app delegate
from the docs:
If the application is running in the foreground, there is no alert,
badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.

Local notification stopped appearing?

I recently did an app for client which includes local notifications. In the original version the notifications were working absolutely fine. But when the client tried to release another version, changed only image and DB resources no change in the code, the notifications stopped working. Right now I've been trying to debug the code but found nothing at all. Everything seems fine but no notifications. Meanwhile if I run the earlier build, it works perfect. Can you please help me out what could be wrong? Thanks very much for your time.
NSTimeInterval diffTimeIntervalSinceNow = timeInterval; //time-interval after which notification should appear
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:diffTimeIntervalSinceNow]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
//[localNotification setRepeatInterval:notificationInterval];
[localNotification setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:alertBody]; //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]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
[localNotification release];
That's the code I've been using. But that's the exact same code which is working in the earlier version.
I Corrected your Code:-
try this:- in Button action or inside in any Method
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSTimeInterval diffTimeIntervalSinceNow = timeInterval; //time-interval after which notification should appear
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:diffTimeIntervalSinceNow]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
//[localNotification setRepeatInterval:notificationInterval];
[localNotification setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:alertBody]; //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]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
[localNotification release];
for this working:- check this method in App delegate getting some event or not
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
For cancel all notification use this in
May be your client also modify the alertBody, and the new alertBody's length is too long?

Clearing the badge when received Push Notification

How can I clear the badge which appears on application icon when I receive Push Notification? I want to clear it once user has either tapped on "View" of Push notification alert or has tapped on the app icon.
I suspect you are talking about the SpringBoard's badge:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
Badge count set Zero
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
Cancel all local notifications with this code:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Cancel one local notification with this line of code:
[[UIApplication sharedApplication] cancelLocalNotification:theNotification];
here theNotification is a UILocalNotification object, so in order to cancel a specific notification, you need to hold on to it's UILocalNotification.
Check this.
For Mac OS X Lion, it's:
[NSApp dockTile].badgeLabel = #"";
(Lion supports badge-type push notifications.)
From Apple's documentation, set the application.applicationIconBadgeNumber to the number you want displayed on the badge. If you set it to 0, it will be cleared.
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
Reference - Scroll down to the Handling Local and Remote Notifications section just above listing 2.4