Iphone presentLocalNotificationNow不触发警报和声音,而应用程序在后台

我有一个应用程序注册的位置更新,运行testing,当我进入一个地区,而应用程序在后台我收到警报通知声音的某个时候。 有时我只能在通知中心看到通知,而且我没有收到任何声音和警报。您可以做些什么来始终获得声音和警报通知?

这是我的观点

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; localNotif.fireDate = nil; localNotif.hasAction = YES; localNotif.alertBody = fbName; localNotif.alertAction = @"View"; localNotif.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication]presentLocalNotificationNow:localNotif]; 

这是应用程序的代表

 - (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification { if (notification) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

{

 facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self]; UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (notification) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } return YES; } 

如果应用程序在后台运行,则本地通知将不会收到警报或声音,因为它是由您的应用程序直接接收的。 在这种情况下,您需要使用presentLocalNotificationNow来呈现通知。

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState applicationState = application.applicationState; if (applicationState == UIApplicationStateBackground) { [application presentLocalNotificationNow:notification]; } }