2013-10-10 70 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]; 
}