UIlocalNotification现在无需启动后台任务即可显示

时间:2013-10-10 06:15:00

标签: iphone ios objective-c uilocalnotification

我有一个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分钟后显示本地通知以显示用户呢?

  3. 我正在使用iPhone 3gsiPhone 4iPhone 5

1 个答案:

答案 0 :(得分: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];
}