0

我有一个 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 通知以显示带有两个按钮的警报,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 3gs,iPhone 4iPhone 5.

4

1 回答 1

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 =@"Message!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
于 2014-05-15T12:09:36.027 回答