0

I write some app that sends local notification with delay and when the notification received i want to decide what to do based on device movement. if device is in move - set the same notification with new delay and stay in background. if device not in move - pop specific view controller.

i do succeed with the "no drive" mode but when the device is in move - i don't really know hot to handle this situation.

here is my code for now, hope for some help thanks!

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"Notification recieved from background...");
    //check if device is in move
    CLLocationManager *locationManager = [[CLLocationManager alloc]init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 50;
    [locationManager startMonitoringSignificantLocationChanges];

    if (locationManager.location.speed > 10) {
        NSLog(@"Device is in drive....");
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];//TODO - Debug Set Real Time Before publish
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        NSLog(@"New Notification sent to device");
        self.window.rootViewController = nil;
        [self.window makeKeyAndVisible];

    }
    else{
        NSLog(@"Device is not in drive....");
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        IGUViewFillDetailsController *vc = (IGUViewFillDetailsController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"IGUViewFillDetailsController"];
        vc.dic = notification.userInfo;
        UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController: vc];
        self.window.rootViewController = vc;
        //[self.window addSubview:vc.view];

        [self.window makeKeyAndVisible];

    }



}
  • What's the problem, is the notification not effective? – gabbler Oct 08 '14 at 15:36
  • i have 2 problems: 1. if the device is in move - the app show's up with the last view wich was open, and i want it to stay in background without any user action. 2. the second problem is that if the device is not in move and i push another view - the current view is show up before the wanted view.... – Yirmi Oppenhime Oct 09 '14 at 20:20

1 Answers1

1

Problem1:

You scheduled a notification and it will show up at the fire date, you can cancel the notification 1 second before the fire date and schedule it again. Say you have this notification to be fired in 10 seconds, you use a NSTimer that will be fired in 9 second, in the selector of the timer you check if the user is moving or not, if he is moving, you cancel the notification and delay it by 10 seconds and schedule it again, remember you also have to update the timer to do the check before next fire date of new notification.

Problem2:

It is the default behaviour, iOS will take a snap shot before app enter background, and when app resign background, it will show this snap shot, you can add a empty view in applicationDidEnterBackground, then this empty view will show up before the wanted view.

gabbler
  • 13,626
  • 4
  • 32
  • 44