UIlocalNotification did now appear without starting background task

断了今生、忘了曾经 提交于 2019-12-01 13:21:40

问题


I have a voip application.

I am using UILocalNotification in my application to alert notification for incoming call when application in background.

When application is in background, after getting an incoming call following function is called.

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

}

This function set a scheduleLocal notification to show an alert with two buttons Cancel and Receive which fires after a second.

Now the problem is:

This local notification appears to user only when a background task started as following

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication* app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{        
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
}

But after 10 minutes background task stops. Then local notification does not appear to user though it fires (A log prints after firing)

I did modify the applicationDidEnterBackground function as following to restart background task for long running. But unable.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    expirationHandler = ^{
        [app endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
        self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
    }
   self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}

Now the questions are:

  1. Is there any way which will fire local notification and appear to user when application is in background?

  2. If background task is mandatory, how could be it long run to show local notification to appear user after 10 minutes?

I am using iPhone 3gs, iPhone 4 and iPhone 5.


回答1:


Try This :

- (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];
}


来源:https://stackoverflow.com/questions/19288356/uilocalnotification-did-now-appear-without-starting-background-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!