iPhone presentLocalNotificationNow不会触发警报和声音,而在后台的应

2019-07-29 04:39发布

我有一个应用程序寄存器位置更新,运行测试,有时当我进入一个区域,而应用程序是在我收到一个带声音的报警通知的背景。 有时我只看到在通知中心通知,我没有收到任何声音和提醒...你能做些什么来总是声音和警报通知?

这就是我在我的观点

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

Answer 1:

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

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


文章来源: Iphone presentLocalNotificationNow not triggering alert and sound while app in background