我有一个 voip 应用程序。
我UILocalNotification
在我的应用程序中使用在后台应用程序时提醒来电通知。
当应用程序在后台时,在接到来电后调用以下函数。
-(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];
}
此函数设置 scheduleLocal 通知以显示带有两个按钮的警报,Cancel
并Receive
在一秒钟后触发。
现在的问题是:
仅当后台任务按以下方式启动时,此本地通知才会向用户显示
- (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];
}
现在的问题是:
当应用程序在后台时,有什么方法可以触发本地通知并出现在用户面前?
如果后台任务是强制性的,那么在 10 分钟后显示本地通知出现用户怎么可能长时间运行?
我正在使用iPhone 3gs
,iPhone 4
和iPhone 5
.