Local notification stopped appearing? - ios5

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?

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.

Handle Multiple Local Notification in IOS SDK

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.

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

Removing UILocalNotification from notification tray programmatically

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray.
I am able to cancel the notification which removes the notifications from
[[UIApplication sharedApplication] scheduledLocalNotifications]
Here is what i need to do
I need to dismiss the UILocalNotification from NotificationTray after the action has been performed(ie after the user taps the notification)
EDIT:
I can remove the notifications from the NSNotificationCenter. I want to remove specific notifications from the Notification Tray .Like the user presses the clear button to clear all the notifications belonging to a particular application.
You can cancel all notifications using:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you want to remove a particular notification, you can use userinfo of notification object, when you create a local notification add a unique ID to that. Later you can use that ID for removing local notification.
For that you can use the following code:
NSString *notificationId = #"id_to_cancel";
UILocalNotification *notification = nil;
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
if([[notify.userInfo objectForKey:#"ID"] isEqualToString:notificationId])
{
notification = notify;
break;
}
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];
I believe I had a similar issue. When the app entered the foreground I attempted to clear past notifications to remove any old notifications from the notifications tray.
I did something like this to grab old notifications and remove them:
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"firDate < %#", [NSDate date]]];
for (UILocalNotification *notification in pastNotifications) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
However, it seems that scheduledLocalNotifications does not include locations whose fire date is already past even though they still appear in notification center.
Calling cancelAllLocalNotifications does seem to remove past notifications as well. So we can grab all the current notifications, cancel everything, and then add the ones we're still interested in back.
// Grab all the current notifications
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Additionally we can do some filtering of the notifications before adding them back if some are no longer needed, and we can grab the active notifications when the app becomes active, store them in an instance variable, and only add them back when the app moves to the background
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
will do some trick too
but if you didnot use applicationIconBadgeNumber, it will not work, so trick is set
applicationIconBadgeNumber first :)
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
If the Application is not running, you will be receiving the Local Notification object in the
-applicationDidFinishLaunchingWithOptions:
like:
UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
or else you can get it in
(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Now you can remove it from the Notification Center using
[[UIApplication sharedApplication]
cancelLocalNotification:notificationToCancel];
// deletes a pushnotification with userInfo[id] = id
-(void)deleteLocalPushNotificationWithId:(NSString*)id{
for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]){
if([[notification.userInfo objectForKey:#"id"] isEqualToString:id]){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
// deletes all fired pushnotifications
-(void)clearLocalPushNotifications{
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
I was fiddling with some code and I was wondering why local notifications are stored in the notification center if the application is in the foreground. It's probably because Apple doesn't know what you are doing with them and honestly doesn't care; so they do their job.
As far as the question is concerned, I do the following:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
NSLog(#"active");
// display some foreground notification;
[application cancelLocalNotification:notification];
}
else
{
NSLog(#"inactive");
}
}
So I just read this thread about how to close/remove all the already fired local notifications from the Notification center, if the user opens the app by clicking the app icon, not the notification. But after all of this, the other scheduled local notification should fire in the future.
Here is my easy solution for this, which should be triggered on application did becomeActive:
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;
I ve tried the [[UIApplication sharedApplication] cancelLocalNotification:notification]; but it did not clear the already fired local notifications from the Notification center (outside of the app).

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