application:didReceiveLocalNotification: performs segue, and gets called again after segue is dismissed

StackOverflow https://stackoverflow.com/questions/23612977

  •  20-07-2023
  •  | 
  •  

Question

My iOS app is scheduling local notifications, which shall take the user into a specified view in my app when tapped. When I tap the notification the app opens (it is in the background) and the methods

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

-(void) handleNotification:(UILocalNotification *)notification
{
    NSString *type = [notification.userInfo objectForKey:NOTIFICATION_TYPE_KEY];
    if([type isEqualToString:TYPE_MESSAGE])
    {
        [self.window.rootViewController performSegueWithIdentifier:@"SeeMessagesSegue" sender:self];
    }
}

is called, as they should. The segue is also performed as intended, and take me to the correct screen. From this screen I have a button that takes me back to the app's root view controller (the main menu), by calling

[self dismissViewControllerAnimated:YES completion:nil];

The view goes away, I come back to the main menu, but then the the application:didReceiveLocalNotification: method is immediately called again, and the segue is again performed.

Why is the method called again (I never get to dismiss the view controller without it getting called again), and how to handle this?

EDIT: Here is the code creating the notifications:

- (void)scheduleNotificationWithMessage:(Message *) msg
{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = msg.fireDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = msg.text;
    localNotif.alertAction = NSLocalizedString(@"notification_open_text", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:msg.type forKey: NOTIFICATION_TYPE_KEY];
    localNotif.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Was it helpful?

Solution

The problem was that I created a new notification in the root view controller's viewDidAppear, and since this notification was back in time, it appeared immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top