Realtime Updates with the Android SDK

Getting realtime pull / push going is pretty straightforward.

Simply add TT.getInstance().startService() and TT.getInstance().stopService() calls at the times that your application comes to the foreground (after a user is authenticated of course) and when the application goes into the background (if necessary).

One way to achieve this is via Lifecycle Observer registered within your Application class, for example:

public class RealTimeEventsTracker implements LifecycleObserver {
private boolean isForegrounded = false;
private boolean isServiceBound = false;
public RealTimeEventsTracker() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onForeground() {
Timber.v("onForeground");
isForegrounded = true;
TT.getInstance().getAccountManager().setPresenceStatus(true);
if (!TT.getInstance().getAccountManager().isLoggedIn()) return;
// If the SSE manager is already connected, set the online presence to available.
if (TTService.isSSEConnected()) {
Timber.v("onActivityStarted RealTimeEvents is already connected set presence to online");
TT.getInstance().getAccountManager().setOnlinePresence(true);
}
startRealTimeEventsService();
bindService();
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onBackground() {
// Changing the online presence to be away since the app is now backgrounded.
Timber.v("onBackground");
isForegrounded = false;
TT.getInstance().getAccountManager().setPresenceStatus(false);
if (!TT.getInstance().getAccountManager().isLoggedIn()) return;
unbindService();
// If the SSE manager is already connected, set the online presence to away.
if (TTService.isSSEConnected()) {
TT.getInstance().getAccountManager().setOnlinePresence(false);
}
stopRealTimeEventsService();
}
private void startRealTimeEventsService() {
try {
TT.getInstance().startService();
} catch (IllegalStateException e) {
String errorMessage = "startRealTimeEventsService: Error starting service";
Timber.e(e, errorMessage);
}
}
private void stopRealTimeEventsService() {
TT.getInstance().stopService();
}
}