现在显示的UIlocalNotification没有启动后台任务

我有一个voip应用程序。

我在我的应用程序中使用UILocalNotification来警告在后台应用程序中传入呼叫的​​通知。

当应用程序在后台时,获得来电后将调用以下function。

 -(void)showLocalNotification:(NSNotification *)notification { NSDictionary *dic = notification.userInfo; NSString *msg = [dic objectForKey:@"msg"]; NSString *sound = [dic objectForKey:@"sound"]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; UILocalNotification *_localNotification = [[UILocalNotification alloc]init]; //setting the fire dat of the local notification _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; //setting the time zone _localNotification.timeZone = [NSTimeZone defaultTimeZone]; //setting the message to display _localNotification.alertBody = msg; //default notification sound if ([sound length]==0) { _localNotification.soundName = UILocalNotificationDefaultSoundName; } else { _localNotification.alertAction = NSLocalizedString(@"Receive", nil); _localNotification.soundName = @"myringtone.aif"; } //displaying the badge number _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1; //schedule a notification at its specified time with the help of the app delegate [[UIApplication sharedApplication]scheduleLocalNotification:_localNotification]; } 

此function设置一个scheduleLocal通知来显示一个警报,其中两个buttonCancelReceive在一秒钟后触发。

现在的问题是:

只有当后台任务开始时,此本地通知才会显示给用户,如下所示

 - (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; } 

但10分钟后,后台任务停止。 然后,本地通知不会显示给用户(虽然它会触发)

我修改了applicationDidEnterBackground函数,如下重新启动后台任务,长时间运行。 但无法。

 - (void)applicationDidEnterBackground:(UIApplication *)application { expirationHandler = ^{ [app endBackgroundTask:self.bgTask]; self.bgTask = UIBackgroundTaskInvalid; self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; } self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; } 

现在的问题是:

  1. 有没有什么办法会触发本地通知,并在应用程序在后台时显示给用户?

  2. 如果后台任务是强制性的,10分钟后如何显示本地通知出现用户呢?

我正在使用iPhone 3gsiPhone 4iPhone 5

尝试这个 :

 - (void)applicationDidEnterBackground:(UIApplication *)application { __block UIBackgroundTaskIdentifier bgTask ; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; pollingTimer2 = [NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(process) userInfo:nil repeats:YES]; } -(void) process { [self didLocalNotification]; } -(void)didLocalNotification { [[UIApplication sharedApplication]cancelAllLocalNotifications]; UILocalNotification *localNotification = [[UILocalNotification alloc] init]; if (localNotification == nil) { return; } NSLog(@"calling didLocalNotification"); localNotification.applicationIconBadgeNumber =0; localNotification.alertBody =@"Message!"; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; localNotification.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }