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 ?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With