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
Related
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.
I am making an app in which I am using apple push notification. When my app is in background state then I am able to receive notification as a banner but when my app is in active state then I am able to show an alert that you have received a notification through this code : -
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
} else {
//Do stuff that you would do if the application was not active
}
}
but I want to show notification as a banner. What I'll do for it? Please suggest.
You can't, if your app is active(running in the foreground) the push notification is directly send to your app. The notification center will not handle the notification.
You will have to implement some kind of banner your self.
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?
If my App receives a UILocalNotification when it is active, I want to show my own UIAlertView and dismiss the system alert. Here's what I'm doing:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alert = ...
[alert show];
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
My own alert shows up, but the system alert somehow remains in the system and is shown as soon as I exit my App.
What am I doing wrong?
Instead of setting [[UIApplication shareApplication] cancelLocalNotification:notification];
set it as [notification setFireDate:nil];.
I have implemented Apple Push Notification service using Urban Airship and successfully received the notification in my application. If i receive the notification the alert view comes, If i click the view button in alert view, it goes to start the application. Generally it happens in APNS. But my client wants, If any updates happened in the RSS feed and alert view comes, If we click the view in alert view, it should go to the particular feed in the application, doesn't start the application. SO is it possible to do that?. Is any possible to write the events for the particular alert view buttons in my application.
Here my sample code is,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
[window addSubview:viewcontrollers.view];
[window makeKeyAndVisible];
NSLog(#"remote notification2: %#",[launchOptions description]);
return YES;
}
In this method didFinishLaunchingWithOptions, i cant get the dictionary values and it always get the Null Value. Is any possible to get the dictionary value in this method(Notification comes).
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *message = [userInfo descriptionWithLocale:nil indent: 1];
NSLog(#"The message string is %#",message);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Remote Notification" message: message delegate: nil cancelButtonTitle: #"ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
In this method, i could get the dictionary value. But this method calls only,if any update happens while running in the application.
Please guide me!
Thanks
It is indeed possible to do that. In your application:didReceiveRemoteNotification method you'll be passed an NSDictionary with all of the push notification's data. What you'll want to do is send some ID or URL in the payload to Urban Airship. Something like:
{
"aps": {
"alert": "New RSS entry"
},
"entry_id": "XYZ123"
}
And then you can write code to go and fetch the proper feed entry in your application.
When the application isn't running or has been terminated by the system and the app is launched via the notification:
In this case you have to get the notification dictionary (which is itself a value of the launchOptions):
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006786-CH3-SW18
I imagine the code would be something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary *remoteNotification = (NSDictionary *) [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification != nil) {
NSString *message = [remoteNotification descriptionWithLocale:nil indent: 1];
}
}