Iphone presentLocalNotificationNow not triggering alert and sound while app in background

隐身守侯 提交于 2019-12-21 21:19:41

问题


I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert... What can you do to always get the sound and the alert notification ?

this is what i have in my view

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

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];

and this is the 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:


If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your application. In that case, you need to present the notification using presentLocalNotificationNow.

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


来源:https://stackoverflow.com/questions/10369945/iphone-presentlocalnotificationnow-not-triggering-alert-and-sound-while-app-in-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!