Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase Messaging: How Update UI from onMessageReceived()

I have successfully implemented Firebase messaging in my app. Works great in the background and onMessageReceived() gets called when the app is in the foreground (yippee!).

The issue I have is that I need to update the UI dynamically and I am stuck on the best way to achieve this. I don't want to send the user an in-app notification (as the sample code shows), I'm not sure I want to send a broadcast, all I want to do is to access the MainActivity in order to call a method already there, however I have no reference to the MainActivity in the service.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification().getBody() != null) {
        Log.d(LOG_TAG, "Message received: " + remoteMessage.getNotification().getBody());
    } else {
        Log.d(LOG_TAG, "Message received: " + remoteMessage.getData().get("message"));
    }

    // Call method in MainActivity
    // <<< What goes Here?>>>>

}

This seems a simple use case but I can't find anything online to help.

Thanks in advance

like image 458
redPanda Avatar asked Mar 31 '17 12:03

redPanda


2 Answers

Yes, you can update UI and pass value to your activity by using Local Broadcast

In your onMessageReceived() Firebase Service.

broadcaster = LocalBroadcastManager.getInstance(getBaseContext());

   Intent intent = new Intent(REQUEST_ACCEPT);
   intent.putExtra("Key", value);
   intent.putExtra("key", value);
   broadcaster.sendBroadcast(intent);

and register local Broadcast in your Activity or fragment method

 @Override
    public void onStart() {
        super.onStart();
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver((receiver),
                new IntentFilter(PushNotificationService.REQUEST_ACCEPT)
        );
    }



    @Override
        public void onStop() {
             super.onStop();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);

        }

and Handle Your update event like this, do your update UI work Here, it will call automatically when notification received and onMessageReceived() send a broadcast.

receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String   value= intent.getStringExtra("key");
                    String value= intent.getStringExtra("key");

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };
like image 98
Nishchal Sharma Avatar answered Nov 15 '22 05:11

Nishchal Sharma


I believe you should send a Local Broadcast with the data and register a receiver wherever you want that data to be utilised. This is a very good design pattern(Observer) as it decouples your Activity from the Service.

If the activity wants to do something with the data it will, else it won't. They are both separate entities and it would be much easier to maintain this code in the future, as far as I know.

Hope this helped.

like image 20
Dushyant Singh Shekhawat Avatar answered Nov 15 '22 05:11

Dushyant Singh Shekhawat