Это может помочь. Эта логика разработана для обеспечения того, чтобы ваше уведомление срабатывало только один раз, и одно и то же предупреждение отображается независимо от того, находится ли приложение на переднем или заднем плане при его запуске.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
//detect if app was launched by notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
//trigger our custom notification handler
[self handleLocalNotification:notification];
}
return YES;
}
- (void)handleLocalNotification:(UILocalNotification *)notification
{
//this is our custom method to handle the in-app notification behaviour
[[[[UIAlertView alloc] initWithTitle:@"You have a notification"
message:@"Yay!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//this standard application delegate method is called under the following circumstances
//1. the app is running in the foreground when the notification fires
//2. the app was running in the background when the notification fired, and the user pressed the action button
//confusingly, if the app was not running when the notification fired, this method won't be called at startup
//automatically and you need to check for the notification key in the launch options instead
if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground)
{
//this code emulates the same behaviour as when a local notification is received
//when the app is not running (except for playing the sound)
//store notification temporarily
[lastNotification release];
lastNotification = [notification retain];
//get button labels
NSString *actionButton = nil;
NSString *cancelButton = @"Close";
if (notification.hasAction)
{
actionButton = (notification.alertAction)? notification.alertAction: @"View"; //defaults to "View" if nil
cancelButton = @"OK";
}
//show alert
[[[[UIAlertView alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] //name of application
message:notification.alertBody
delegate:self
cancelButtonTitle:cancelButton
otherButtonTitles:actionButton, nil] autorelease] show];
}
else
{
//trigger our custom notification handler
[self handleLocalNotification:notification];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
//trigger our custom notification handler
[self handleLocalNotification:lastNotification];
}
}