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

时间:2012-04-29 05:30:53

标签: ios uialertview uilocalnotification

我有一个App寄存器用于位置更新,运行测试,有时我在应用程序处于后台时进入某个区域时会收到声音警报通知。有时我只在通知中心看到通知,我没有收到任何声音和警报...... 您可以做些什么来始终获得声音和警报通知?

这就是我的观点

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

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];

这是app delegate

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

1 个答案:

答案 0 :(得分:3)

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

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