Iphone UIlocalNotification现在没有启动后台任务就出现了
我有一个voip应用程序 我在我的应用程序中使用Iphone UIlocalNotification现在没有启动后台任务就出现了,iphone,ios,objective-c,uilocalnotification,Iphone,Ios,Objective C,Uilocalnotification,我有一个voip应用程序 我在我的应用程序中使用UILocalNotification,在应用程序处于后台时,为传入呼叫发出警报通知 当应用程序在后台时,在收到传入呼叫后,调用以下函数 -(void)showLocalNotification:(NSNotification *)notification { NSDictionary *dic = notification.userInfo; NSString *msg = [dic objectForKey:@"msg"];
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分钟后,后台任务停止。然后,虽然本地通知触发,但用户不会看到它(触发后会打印日志)
我确实修改了applicationidenterbackground
函数,如下所示,以重新启动后台任务以进行长时间运行。但是不能
- (void)applicationDidEnterBackground:(UIApplication *)application
{
expirationHandler = ^{
[app endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
现在的问题是:
iphone3gs
、iphone4
和iphone5
试试这个:
- (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];
}