Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 831
Yogesh Rathi Avatar asked Aug 05 '16 04:08

Yogesh Rathi


1 Answers

I think you have to try with BroadcastReceiver. This is how you send a message from your FCMListenerService:

  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);
         }
  }

And then you can try to catch it this way, using broadcast receiver in your 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);
like image 140
Thomas Avatar answered Nov 13 '22 08:11

Thomas