Updating Activity when it comes back to the Foreground after data has already been retrieved
Asked Answered
H

0

2
  • I have implemented a Service that receives notifications sent from the server.
  • I'm currently using a broadcast receiver to send data. The Broadcast Listener is updating the Activity just fine. However, when data is sent when the Activity is not on "onResume" state, the data is not received.
  • I got interested in using RXjava because I believe it could resolve the issue but don't know where to start.

The problem: When the activity is not on the foreground, the Activity is not updating.

Not in the foreground: Meaning that I have called unregisterReceiver to unregister in my onPause method.

My current user case: I'm using a Service to intercept notification sent from server using FCM

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "[" + MyFirebaseMessagingService.class.getName() + "]=";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
        }

    /**
     * Handle time allotted to BroadcastReceivers.
     */
    private void handleNow() {
        Log.d(TAG, "Short lived task is done.");
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            // Handle message within 10 seconds
            handleNow();
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // <-----------RX JAVA Observable Implementation----------->
        // I would like to use Rx java to send data to my activity ONLY if the activity is in the foreground
        // because data will be used to update the UI
    }

    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }
}

I looked into RXjava and I find out that the library takes care of asynchronous operations, but what I'm curious about is:
if there is any functions within RXjava that will automatically update that data once the Activity is back on the foreground?

If not, what are the other alternatives?

Maybe this question has already been answered but I could not find a precise user case to what I'm trying to achieve in here. Also, I might be asking the question wrongly, but that just explains my confusion here

Host answered 20/11, 2018 at 20:37 Comment(4)
To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.Migration
notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefsJaredjarek
I went ahead and used RxJava that does the job using the function onNext on the observable and observer.Host
I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.Tricolor

© 2022 - 2024 — McMap. All rights reserved.