How to add action to pushnotification alertview? - iphone

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

Related

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

Show Alert in clickedButtonAtIndex?

i need to show a confirm alert after the user press buttonIndex 1 but... if i use popViewcontroller in clickedButtonAtIndex it crash without errors.
The problem is that
[self.navigationController popViewControllerAnimated:YES];
is called before second Alert click...
how to fix?
This is my code:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"OK!"
message:#"Completed"
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
}
}
Set the tag properties of the two UIAlertViews to 1 and 2, respectively. Then, in the delegate method, use if statements to check the tag of the UIAlertView argument.
Example:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
//check the button index
//create and display the other alert view (set the tag property here to 2)
}
else if (alertView.tag == 2)
{
//pop the view controller
}
}

alertView didDismissWithButtomIndex is not called after a click on the alertView's buttom

I have recently begun my studies on iOS development so forgive me if I am asking something too obvious.
When my application's view loads it checks the configurations for some keys and if there's no value for these keys the application should display an alert and quit.
First of all, I have implemented the UIAlertViewDelegate:
#interface FirstViewController : UIViewController <UIAlertViewDelegate> {
...
And then checked for the settings:
- (void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *url = [defaults stringForKey:#"url"];
NSString *apiKey = [defaults stringForKey:#"api-key"];
if([url length] < 1 || [apiKey length] < 1){
UIAlertView *dialog = [[[UIAlertView alloc]
initWithTitle:#"Not properly configured"
message:#"This application was not properly configured. Please configure the application on your iPhone settings."
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil]
autorelease];
[dialog setTag:1];
[dialog show];
}
[url release];
[apiKey release];
[super viewDidLoad];
}
I understand that the method alertView didDismissWithButtomIndex should be called after the alertView's dismiss but for some reason this method is never called in my code.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtomIndex:(NSInteger)buttomIndex {
if([alertView tag] == 1){
exit(0);
}
}
Any ideas on why this is happening?
didDismissWithButtonIndex is misspelled, you snuck an ‘m’ in there instead of ’n’.
You are listening for the wrong method, you should implement :
alertView:clickedButtonAtIndex:
In the doc you can read that the didDismissWithButtomIndex is called when dismissWithClickedButtonIndex:animated: is called on the alertView.
alertView:didDismissWithButtonIndex:
Sent to the delegate after an alert view is dismissed from the screen.
So for your code to work you should implementsomething like :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == ...) {
// do something
}
}
PS: You shouldn't call exit(0), it is a bad practice on iOS to force an application to quit. User are supposed to quit the app with the home button.

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