UILocalNotification does not fire after 10 minutes in background

I have a VoIP application. Which is working fine. Call is working in foreground and background.

Following steps are done:

  • GCDAsyncSocket background VOIP
  • Use SIP in iPhone app
  • SIP library for iOS with non-GPL license
  • VoIP socket on iOS - no notifications received
  • How do I make my App run an NSTimer in the background?
  • ios audio unit remoteIO playback while recording
    1. UIBackgroundModes => voip in Info.plist

    2. Configured one of the app’s sockets for VoIP usage.

    3. Before moving to the background, setKeepAliveTimeout:handler: is called

    4. Configured audio session to handle transitions to and from active use.

    5. To ensure a better user experience on iPhone, used the Core Telephony framework to adjust behavior in relation to cell-based phone calls;

    6. To ensure good performance for VoIP app, used the System Configuration framework to detect network changes and allow app to sleep as much as possible.

    Now the thing is when application is in background and a call comes then UILocalNotification fires for the following. And user can see a notification with two buttons CANCEL and RECEIVE

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
            [application endBackgroundTask: bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            while ([application backgroundTimeRemaining] > 1.0) {
                NSString *friend = [self checkForIncomingChat];
                if ([friend length]>0) {
                    [[UIApplication sharedApplication] cancelAllLocalNotifications];
                    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                    if (localNotif) {
                        localNotif.alertBody = [NSString stringWithFormat: NSLocalizedString(@"%@", nil), friend];
                        localNotif.alertAction = NSLocalizedString(@"Receive", nil);
                        localNotif.soundName = @"alarmsound.caf";
                        localNotif.applicationIconBadgeNumber = 1;
                        [application presentLocalNotificationNow:localNotif];
                        friend = nil;
                    }
                }
                sleep(1);
            }
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
     }
    
     - (NSString *) checkForIncomingChat {
    
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSString *incall = [prefs objectForKey:@"incall"];
        if ([incall length]>0) {
            [prefs setObject:@"" forKey:@"incall"];
            return incall;
        }
        return @"";
    };
    

    Now the problem is:

    After going to the background by pressing home button application fires UILocalNotification if any call comes within 10 minutes.

    After 10 minutes if any call comes it is running in the background. UILocalNotification does not fire, so user can not know anything.

    It happens because background task stops after 10 minutes.

    How can I manage it or extend background task for long running or restart background task.

    More and more answer I have found after searching but nothing works for long running background task.

    Please anybody help me. I am trying it since 2 weeks.

    Solutions Collect From Internet About “UILocalNotification does not fire after 10 minutes in background”

    It sounds like you have a VoIP socket that you are receiving the call on, so rather than looping and polling for call state, you can just present the local notification at the point that you read the data off the socket.

    If the VoIP control socket is TCP, and marked with the appropriate ...NetworkServiceTypeVoIP key, your app will automatically get woken up from suspension for 10 seconds, at which point you can present the local notification.

    See Configuring Sockets for VoIP Usage for more information.

    Once that is done, all of the code that you shared above can be removed.