2013-10-10 36 views
0

我有一个voip应用程序。现在UIlocalNotification没有开始后台任务

我在我的应用程序中使用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通知以显示一个警报,两个按钮CancelReceive其中第二后触发。

现在的问题是:

这本地通知似乎用户,只有当后台任务开始如下

- (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

回答

0

试试这个:

- (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 [email protected]"Message!"; 
    localNotification.soundName = UILocalNotificationDefaultSoundName; 
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}