Push notification alert view action? - iphone

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

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.

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

How to navigate from app delegate class in window based iPhone application. How to write pushViewcontroller method in app delegate class

In my window base application I need to navigate to informationview from my appdelegate when i click on alert view button.
alert view works with NSlog.
But i need to push to the other view for this purpose i used
[self.navigationController pushViewController:info animated:YES];
but it doesn't pushes. just nslog only prints
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//To count the number of launches
NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:#"numOfCalls"];
[[NSUserDefaults standardUserDefaults] setInteger:i+1 forKey:#"numOfCalls"];
NSLog(#"the number of active calls are %d",i%3);
if(i%3==0 && i!=0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"you might prefer MedChart+" message:#"Get it now for more options" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"canceled");
}
else if (buttonIndex == 1)
{
NSLog(#"Pushed to the information view ");
InformationViewCotroller *info = [[InformationViewCotroller alloc]initWithNibName:#"InformationViewCotroller" bundle:nil];
[self.navigationController pushViewController:info animated:YES];
}
}
(dont consider 'i' values it is part of my logic).
Thanks in advance
Before Navigate to any viewController , set the RootController for your navigationController of appDelegate.
Add navigationController.View as subview of window.Then your root controller will be the first ViewController.from there you can push to any viewController.

UILocalNotification when the application is active?

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

Alert with 2 buttons

I'm going to have a link to a website in my app. The user will click on a button that says Website and an Alert will appear with 2 buttons. One of the buttons is just going to be a cancel button and the other button is going to open the website.
Could you help me with this?
Thanks!
put this into your header file:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
put this into the class with your alert:
- (void)alertOKCancelAction {
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Open?" message:#"Open Website?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
alert.tag = 1;
[alert show];
[alert release];
}
add this method:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if(alert.tag == 1)
{
if(buttonIndex == alert.cancelButtonIndex)
{
NSLog(#"cancel");
}
else
{
NSLog(#"ok");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
}
}