0

I am new to iPhone Programming .I have developed one application for checking user entered into a particular region.But i need to check in Background.In background i am checking but the problem is repeating the UILocalNotification alerts . So how to prevent the repeated UILocalNotifications

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    NSLog(@"running in background ...");

    [self checkRegionEntered];

    CurrentlattitudeValue1 =newLocation.coordinate.latitude;
    CurrentlongitudeValue1=newLocation.coordinate.longitude;
}

-(void)checkRegionEntered
{

    if ([testRegion containsCoordinate:currentCoordinates]) 
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            UILocalNotification *notif = [[cls alloc] init];
            NSDate *now = [NSDate date];
            [notif setFireDate:now];

            if([Obj.NotesGeo length])
                [notif setAlertBody:Obj.NotesGeo];
            else 
            {
                [notif setAlertBody:[NSString stringWithFormat:@", you have arrived at %@",Obj.NameGeo]];
            }

            [notif setAlertAction:@"Launch"];
            notif.soundName=[NSString stringWithFormat:@"%@.wav",Obj.Ringtone1];//[NSString stringWithFormat:@"%g",Obj.LatitudeGeo]
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%f",Obj.LatitudeGeo] forKey:kRemindMeNotificationDataKey];

            notif.userInfo = userDict;
        }
    }
}
X Slash
  • 4,133
  • 4
  • 27
  • 35
Hariprasad.J
  • 307
  • 1
  • 12

1 Answers1

1

This may help. This logic is designed to ensure that your notification will only fire once, and that the same alert is displayed regardless of whether the app is in the foreground or background when it fires.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    //detect if app was launched by notification
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification)
    {
        //trigger our custom notification handler
        [self handleLocalNotification:notification];
    }

    return YES;
}

- (void)handleLocalNotification:(UILocalNotification *)notification
{
    //this is our custom method to handle the in-app notification behaviour
    [[[[UIAlertView alloc] initWithTitle:@"You have a notification"
                                 message:@"Yay!"
                                delegate:nil
                       cancelButtonTitle:@"OK"
                       otherButtonTitles:nil] autorelease] show];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //this standard application delegate method is called under the following circumstances
    //1. the app is running in the foreground when the notification fires
    //2. the app was running in the background when the notification fired, and the user pressed the action button
    //confusingly, if the app was not running when the notification fired, this method won't be called at startup
    //automatically and you need to check for the notification key in the launch options instead

    if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground)
    {
        //this code emulates the same behaviour as when a local notification is received
        //when the app is not running (except for playing the sound)

        //store notification temporarily
        [lastNotification release];
        lastNotification = [notification retain];

        //get button labels
        NSString *actionButton = nil;
        NSString *cancelButton = @"Close";
        if (notification.hasAction)
        {
            actionButton = (notification.alertAction)? notification.alertAction: @"View"; //defaults to "View" if nil
            cancelButton = @"OK";
        }

        //show alert
        [[[[UIAlertView alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] //name of application
                                     message:notification.alertBody
                                    delegate:self
                           cancelButtonTitle:cancelButton
                           otherButtonTitles:actionButton, nil] autorelease] show];
    }
    else
    {
        //trigger our custom notification handler
        [self handleLocalNotification:notification];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        //trigger our custom notification handler
        [self handleLocalNotification:lastNotification];
    }
}
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • You don't need to create a BOOL to track whether you are backgrounded or not. You can just check the [applicationState](http://stackoverflow.com/a/8292048/154803) to see if you are running in the background. – progrmr Jan 20 '12 at 19:09
  • Good call - I've updated the code sample to use applicationState instead. – Nick Lockwood Jan 21 '12 at 08:23
  • Its far better check for application state whether UIApplicationStateActive. If yes, do your stuff else leave that. – Abdul Yasin Sep 20 '13 at 10:05