8

Will the local notification show up when the app is in foreground and currently running in iPhone SDK?

Dip Dhingani
  • 2,499
  • 2
  • 20
  • 18

5 Answers5

9

No, you will receive a the notification in the appdelegate.

- (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification {
    //Place your code to handle the notification here.
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Thanks a lot. Now, I've kept an UIAlertView in application didReceiveLocalNotification method so that I can use it in place of notification when the app is already running. But when the app is in background and the notification is fired and when app comes to foreground, this method is called and that alertView comes up. Can you tell me how I can avoid this. – Dip Dhingani Jul 28 '11 at 10:03
  • Okay we have applicationWillEnterForeground: method for that. Sorry stupid question! Thanks a lot. – Dip Dhingani Jul 28 '11 at 10:12
  • Not a stupid question - you just gave me the answer I was looking for :-) – Jonathon Horsman Aug 16 '11 at 14:01
3

I made an lib to make an animation almost as same as local notification's.

Check this: https://github.com/OpenFibers/OTNotification

Demo: enter image description here

enter image description here

And you can post a new message to this lib when you received a message in

- (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification
{
    OTNotificationManager *notificationManager = [OTNotificationManager defaultManager];
    OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init];
    notificationMessage.title = [self notificationTitle];
    notificationMessage.message = @"A notification. Touch me to hide me.";
    [notificationManager postNotificationMessage:notificationMessage];
}
OpenThread
  • 2,096
  • 3
  • 28
  • 49
2

The accepted anser is right, but it's not enough to receive all notifications and show something to user from

- (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification {

You have to check, is it current notification or not. Sometimes there is fires another notifications (when you cancel them, for example). So, you have to check, that is what you except:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f)
    {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notification alert", @"")
                                    message:notification.alertBody
                                   delegate:self
                          cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];    
    }
}
skywinder
  • 21,291
  • 15
  • 93
  • 123
0

Swift 2.2:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    var state = application.applicationState
    if state == .Active {
        // handle the notification, e.g. show an alert 
    } 
}

Swift 3.0:

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    var state: UIApplicationState = application.applicationState
    if state == .active {
        // handle the notification, e.g. show an alert
    }
}
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
0

if your app is currently in foreground the following function will be called in your Delegate:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation

you can then decide to show an alertview, but the standard one will not show up by itself

Bastian
  • 10,403
  • 1
  • 31
  • 40