0

i know this question had been asked before Show AlertView dialog from UILocalNotification but due to unsatisfied answers and different scenarios i mention my question what i am trying to do is to check if the state of the application is in the foreground the app should display an alertView or alertControl with actions and Creates a notification with a given name, sender, and information and posts it to the receiver, here we put our self in front of two solution

First Solution:

to use the alert view with his delegate method like How to create an alert box in iphone?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
//That will display the message.

//Then to check whether they tapped delete or cancel, use this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        //delete it
    }
}

or using the Second Solution:

 //    we need to show alertDialogue //
    UIAlertController* alert = [UIAlertController 

alertControllerWithTitle:@"Reminder"
                                                                       message:notification.alertBody
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 
                                                             }];
        
        UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
                                                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
                                                                  }];
        [alert addAction:viewDetailsAction];
        [alert addAction:cancelAction];

the full code to understand my problem :

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    
     if (application.applicationState == UIApplicationStateActive)
    {
        
        
        
        
        


        
     //    we need to show alertDialogue //
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Reminder"
                                                                       message:notification.alertBody
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 
                                                             }];
        
        UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
                                                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
                                                                  }];
        [alert addAction:viewDetailsAction];
        [alert addAction:cancelAction];
   // [self presentViewController:alert animated:YES completion:nil];
   //this line cause not visible interface inside appdelegate//
        
    }
    else
    {
        reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
    }
    
    
}

as you can see i am checking on the didRecieveLocalNotification method if the app is in the foreground or in whatever different state is. if the app inside the background create a local notification with name and sender if is the foreground create alertController or alertView with a local notification with name and sender

and i could not use the first solution because its delegate method and that will prevent me from accessing the userInfo of the local notification and for the second solution i could not use it due to error that said no visible inferface for AppDelegate declare the selector presentViewController:animated:completed

thanks in advance and Happy Coding :D

Community
  • 1
  • 1
Zakaria Darwish
  • 358
  • 5
  • 22
  • Your question is quite confusing... what do you really want to ask here? Is your error about 'didReceiveLocalNotification' not firing so you can't check for the app's state and trigger those alerts? if so, then you need to ask for user permission to use notification local or remote by setting 'registerUserNotificationSettings', this is required. – eNeF Aug 17 '15 at 11:09
  • nooooo the localNotification is working fine just i am checking the state of the app for 2 reason if the app is in the foreground local notification should be displayed from a uialertController or uiAlertView and if in the background the app fire the local notification and in both cases i need the user info of the notification because when the user click on the notification or on the viewDetails button inside the uialertView or alertController the app should move to view the details – Zakaria Darwish Aug 17 '15 at 11:12
  • Your question doesn't really make sense. As Jasper says in his answer, you will receive a local notification if you in the foreground and the user is actively using your app, or if the user responds to a system local notification for you app by invoking it (in the background). In the second case the user already saw and responded to a local notification so it doesn't make sense to create another one. – Duncan C Aug 17 '15 at 11:17

1 Answers1

0

There are two app states when you can receive a UILocalNotification:

When the app is currently in foreground the following method will be fired:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [self showBannerForNotification:notification];
    application.applicationIconBadgeNumber = 0;
}

When the app is in background you will need to check on startup if the app was opened from a UILocalNotification (the user swiped right on the UILocalNotification and the app opened):

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Do your default app startup code

    //Check if the app opened from a LocalNotification
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification)
    {
        //App was opened from a UILocalNotification
        //Handle accordingly
    }

    return YES;
}
Jasper
  • 7,031
  • 3
  • 35
  • 43