Yogesh Rathi Yogesh Rathi - 3 months ago 310
Java Question

Calling activity class method from FirebaseMessagingService class


How to call activity class method from fcm service.


I already tried this way Calling activity class method from Service class

but in fcm service
onBind
method is final so we can not overwrite, so any other way to call activity class method from fcm service.

for reference some code how to implement fcm.

public class FCMListenerService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
}
}


When my activity is running and fcm notification came then i want to update code? is there any way to handle this requirement ?

Answer
  public class FCMListenerService extends FirebaseMessagingService {

        public static final String INTENT_FILTER = "INTENT_FILTER";
         @Override
         public void onMessageReceived(RemoteMessage message) {
              Intent intent = new Intent(INTENT_FILTER);
              sendBroadcast(intent);
         }
  }

where i want to handle broadcast this activity.

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUi();
    }
};

Do not forgot to register / unregister your receiver in onCreate() / onDestroy() method from your activity.

onCreate()

registerReceiver(myReceiver, new IntentFilter(FCMListenerService.INTENT_FILTER));

onDestroy()

unregisterReceiver(myReceiver);

This code is useful when you want to update only one or few activity ui.

Comments