Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

iphone - 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:

  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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...