Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pubnub connection stops when application goes into background state iOS 8

Tags:

ios

pubnub

Title says it all. I've looked at this question and also here on the pubnub forums (same question, just different suggestion).

The core of the issue is that as soon as the application suspends, pubnub connectivity is queued and not sent until the app goes back to foreground. It seems to be a reasonable thing to do to send a notification saying that you're going in the background on your channel but it doesn't work.

From my readings I understand that pubnub uses websockets and that it is not allowed in background mode. Even tried to enable VOIP as a background mode with no luck but Location updates bg mode works. However, using this will have my app rejected as I don't use location services.

When running this code

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [PubNub sendMessage:@"Hello from PubNub iOS!" toChannel:self.myChannel;
}

I get this log entry from pubnub (so at least I know the command is ran):

Looks like the client suspended"; Fix suggestion="Make sure that your application is configured to run persistently in background

I have been killing myself over this for a day. One of these days where you start doing something that you think is pretty simple, a 15min thing and it turns into a day of frustration ... You know what I mean :)

like image 240
GrandSteph Avatar asked Dec 06 '14 15:12

GrandSteph


People also ask

What happens when the app is sent to the background?

Should the user accept the phone call, the app will be sent to the background. DidEnterBackground - Called when the app enters the backgrounded state, this method gives an application about five seconds to prepare for possible termination. Use this time to save user data and tasks, and remove sensitive information from the screen.

How does IOS notify when an app changes state?

When an app changes state, iOS notifies the application through event methods in the AppDelegate class: OnActivated - This is called the first time the application is launched, and every time the app comes back into the foreground. This is the place to put code that needs to run every time the app is opened.

How does background processing work in iOS?

iOS regulates background processing very tightly, and offers three approaches to implement it: Register a Background Task - If an application needs to complete an important task, it can ask iOS not to interrupt the task when the application moves into the background.

What is register a background task in iOS?

Register a Background Task - If an application needs to complete an important task, it can ask iOS not to interrupt the task when the application moves into the background. For example, an application might need to finish logging in a user, or finish downloading a large file.


1 Answers

I was actually able to send the messages I needed when the app was about to enter Background. And without enabling any of the background modes.

I took advantage of the background finite task as explained is this tutorial.

- (void)applicationWillResignActive:(UIApplication *)application {

    [PubNub updateClientState:@"MyID" state:@{@"appState":@"Background",@"userNickname":@"MyNickname"} forObject:[PNChannel channelWithName:@"MyChannel"]];

    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background handler called. Not running background tasks anymore.");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }];

}

And implementing the stop background when coming back online

- (void)applicationDidBecomeActive:(UIApplication *)application {

    if (self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
        NSLog(@"Task invalidated");
    }
}
like image 132
GrandSteph Avatar answered Oct 15 '22 11:10

GrandSteph