Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase PresenceManaging iOS

Tags:

ios

firebase

I'm implementing a system based on firebase docs:

[connectionMonitor observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    if([snapshot.value boolValue]) {
        // connection established (or I've reconnected after a loss of connection)

        // add this device to my connections list
        // this value could contain info about the device or a timestamp instead of just true
        Firebase * con = [[Firebase alloc]initWithUrl:[NSString stringWithFormat:@"%@Users/%@/connections/", urlString, currentUserId]];
        Firebase * newConnection = [con childByAutoId];
        [newConnection setValue:@YES];

        // when this device disconnects, remove it
        [newConnection onDisconnectRemoveValue];
    }
}];

Which works fine, if the user fully disconnects, but that's my problem.

I use this system to see if the user is online. If they're not online, I trigger a push notification. If the user closes the app, firebase doesn't disconnect, but it also doesn't receive updates, so on the other end, the user looks like they are still online. For the firebase onDisconnect value to properly set, the user is required to completely close out of the app.

I have resolved this by adding:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [Firebase goOffline];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [Firebase goOffline];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
   [Firebase goOnline];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [Firebase goOnline];
}

Is this normal behavior, or am I doing something wrong?

like image 717
Logan Avatar asked Oct 01 '22 08:10

Logan


1 Answers

This is (currently) expected behavior. Firebase won't trigger the presence actions until the client actually disconnects and iOS will leave the underlying socket connection alive for some period of time (probably less than 5 minutes) after the app goes to the background... so presence will be delayed. It should still definitely happen eventually though.

Your workaround should work fine, or to avoid tearing down the whole connection, you could just set the presence bit to @NO / @YES on going to background / foreground.

I can see how most apps would expect presence to kick in when the app goes to the background, so we may investigate changing this behavior in the future.

like image 97
Michael Lehenbauer Avatar answered Oct 21 '22 13:10

Michael Lehenbauer