开发者

Local Notification in foreground in iPhone SDK

Will the local notification sh开发者_开发百科ow up when the app is in foreground and currently running in iPhone SDK?


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.
}


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

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

Demo:

Local Notification in foreground in iPhone SDK

Local Notification in foreground in iPhone SDK

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];
}


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];    
    }
}


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


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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜