UILocalNotification when the application is active? - iphone

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];.

Related

How to show remote push notification as a banner style in active state of app?

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.

Push notification alert view action?

I have an iphone application in which i am doing the push notification as an alertview.When my application is in the background state the push notification is coming,and when i am clicking on it or unlocking the phone it is directly entering in to the app where i have left it in the forground state.I am adding an action in the alert with a click on view button it is going to another view controller.I Dnt want to enter the application when i am clicking on the notification.I need to show the alertview and when clicking on the view button i need to do my action.Can anybody help me to achieve this.This is my code snippet `-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//check application in forground or background
if(application.applicationState == UIApplicationStateActive)
{
//NSLog(#"FOreGround");
//NSLog(#"and Showing %#",userInfo)
}
else
{
NSDictionary *curDict= [userInfo objectForKey:#"aps"];
UIAlertView *connectionAlert = [[UIAlertView alloc] initWithTitle:#"app" message:[NSString stringWithFormat:#"%#",[curDict objectForKey:#"alert"]] delegate:self cancelButtonTitle:#"View" otherButtonTitles:#"Cancel",nil];
[connectionAlert show];
[connectionAlert release];
[UIApplication sharedApplication].applicationIconBadgeNumber =[[curDict objectForKey:#"badge"] intValue];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"applicationWillEnterForeground");
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"View"])
{
NSArray *mycontrollers = self.tabBarController.viewControllers;
NSLog(#"%#",mycontrollers);
[[mycontrollers objectAtIndex:0] popToRootViewControllerAnimated:NO];
mycontrollers = nil;
tabBarController.selectedIndex = 0;
}
}
You show the UIAlertView to the user, When notification is received then the didReceiveRemoteNotification: function is called.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo :%#",userInfo);
NSString* msg = [userInfo valueForKey:#"aps"];
if (self._VCObj.isViewLoaded && self._VCObj.view.window) {
// viewController is visible don't show.
}
else { // viewController is not visible
[[[UIAlertView alloc]initWithTitle:#"Title" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil] show];
}
}
}
See Tutorial

How to add action to pushnotification alertview?

I have an application in which i am adding push notification.I am showing the push notication as an aletview.with two buttons view and cancell.i need when the user clicks on the view button i need to go to particular viewcontroller .can anybody help me in achieving that?this how i done with notification.`
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"########################################didReceiveRemoteNotification****************************###################### %#",userInfo);
//check application in forground or background
if(application.applicationState == UIApplicationStateActive)
{
//NSLog(#"FOreGround");
//////NSLog(#"and Showing %#",userInfo)
}
else {
NSDictionary *curDict= [userInfo objectForKey:#"aps"];
UIAlertView *connectionAlert = [[UIAlertView alloc] initWithTitle:#"app" message:[NSString stringWithFormat:#"%#",[curDict objectForKey:#"alert"]] delegate:self cancelButtonTitle:#"View" otherButtonTitles:#"Cancel",nil];
[connectionAlert show];
[connectionAlert release];
[UIApplication sharedApplication].applicationIconBadgeNumber =[[curDict objectForKey:#"badge"] intValue];
}
}
`
You just have to implement the UIAlertViewDelegate and call the delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex) {
case 0: // do something
break;
case 1: // do something else
break;
}
}

iphone: Set badge on a tabbarItem when receiving a PUSH message, when the app is inactive

I have a application that uses PUSH. But I have one problem when the application is inactive/in the background.
When the PUSH messages come and the user clicks on Close, the badge is set on the application-icon.
But I also want to set a badge on a tabBarItem.
I have this code that saves the PUSH
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
//Save the PUSH until the app is active.
newPush = [userInfo copy];
}
}
And in:
- (void)applicationDidBecomeActive:(UIApplication *)application
I have the following code:
//Check if there is new PUSH messages.
if (newPush!=nil) {
//There is a new PUSH!
NSInteger badge = [[[newPush objectForKey:#"aps"] objectForKey:#"badge"] intValue];
if (badge > 0) {
//Set badge-numbers to 'badge'
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:[NSString stringWithFormat:#"%d",badge]];
}
else {
//Set badge-numbers to zero
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:nil];
}
}
My code for handling the PUSH when the application is active works fine and the badges are set both on the application-icon and on the tabBarItem.
Someone know what's wrong?
Thanks in advance!
If the application is inactive, didReceiveRemoteNotification is not executed. The only way the notification data can reach your app in this case is if the user taps on the notification to open the app. Then, when the app is launched, you can get the notification data in application:didFinishLaunchingWithOptions: by using this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif];
return YES;
}
return YES;
}

Apple Push Notification Service using Urban Airship in iPhone

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];
}
}