前景中的通知弹出窗口

时间:2014-12-05 12:18:15

标签: ios iphone uilocalnotification

我想在应用处于前台状态时显示通知弹出窗口,并根据代码片段显示alertbody。
当应用处于后台状态时,它完全正常工作。

UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification == nil)
    return;
NSDate *dt = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate date]];
notification.fireDate = dt;
notification.timeZone = [NSTimeZone defaultTimeZone];

notification.alertBody = @"After 10Secs...";
notification.alertAction = @"View";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

1 个答案:

答案 0 :(得分:1)

如果您希望在应用程序位于前台时看到nofication,则

application:didreceiveLocalNotification 您的应用委托方法:

- (void)application:(UIApplication *)application
    didReceiveLocalNotification:(UILocalNotification *)notification
{
    //simply show a alert,but the standard one will not show up by itself
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyAlertView"
        message:notification.alertBody
        delegate:self cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alertView show];
    if (alertView) {
        [alertView release];
    }
}