Pubnub connection stops when application goes into background state iOS 8
Asked Answered
K

1

6

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

Klarrisa answered 6/12, 2014 at 15:58 Comment(6)
Have you enabled VOIP SOCKET? your pList file <key>UIBackgroundModes</key> <array> <string>voip</string> </array> Looks like you did do this. Make sure to email [ [email protected] ] for more deep-dive.Lotetgaronne
This is less of a PubNub issue than an iOS issue. If the app can run in the background, then so can PubNub. Android is much more straight forward and works great with PubNub staying connected in a background service. This is some fairly new background test code for the PubNub iOS SDK. github.com/pubnub/objective-c/tree/master/iOS/tests/… Give that a look over and if you still have questions, contact PubNub Support.Dignify
@CraigConover I came accross this earlier but didn't really deep-dive into it. I thought the background feature was fairly simple and that I was missing something. Looks like it not really straight forward. knowing that is already helpful.Klarrisa
@PubNub yes. VOIP didn't work but location updates did. Problem is I'll get rejected because I don't really use location or VOIP. I was thinking about using HTTP request. Anybody got any luck with that?Klarrisa
You will be able to run an HTTP call while in the background, but not with any greater precision than having PN run while in the background. Getting the app configured to run PN in the background in GPS mode is probably your best bet. If you still encounter a deeper dive, contact us at [email protected]Olcott
Well, what exactly happens when pubnub goes to background and is suspended? I find that pubnub does NOT work after the device goes to sleep. When it comes back from sleep, the shared instance is connected but all messages and status updates are being postponed. Going to background but not sleep works well. It's just when going to sleep and coming back from sleep that this weird behavior happens.Klarrisa
K
2

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");
    }
}
Klarrisa answered 28/1, 2015 at 21:24 Comment(2)
Did the above solution work for you after all, even if you left the app in the background for 30 minutes or more? As far as I know background tasks are finite-length so it means the OS can suspend your app at any time. I'm asking this because I'm experiencing the same issue of postponed messages after the app coming into foreground.Esotropia
My goal was just to send an update when leaving the app. For that purpose it works well since it only takes seconds. Any other task, you are right, will get eventually suspended by the OS.Klarrisa

© 2022 - 2024 — McMap. All rights reserved.