Search code examples
androidfirebasepush-notificationfirebase-cloud-messagingandroid-push-notification

How to find out the current (top) Activity from onMessageReceived (of FirebaseMessagingService)?


I have been searching everywhere but just can't figure how to get the current (top) activity of a running Android application.

Alright, my scenario is I received a Firebase Cloud Messaging Data payload while the app is in the foreground and the ​onMessageReceived (of a subclass of FirebaseMessagingService) is invoked. I need to find out what screen the user is viewing and then decide to dismiss (finish()) it or send some data (via Extras / Bundle) and refresh the view.

So, how do I find out what is the current View / Activity and to discuss or send some data and cause the view to refresh?

Thanks!


Solution

  • Someone else posted the correct answer and then deleted it, so I'll post it again:

    You don't need to determine the top activity. Each of your activities should listen for some signal from the FirebaseMessagingService and take the appropriate action. The signal could be an Intent broadcast on the LocalBroadcastManager, or you could use some kind of event bus.