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

余生长醉 提交于 2019-12-11 03:08:45

问题


  • 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

来源:https://stackoverflow.com/questions/53401134/updating-activity-when-it-comes-back-to-the-foreground-after-data-has-already-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!