7

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 ?

Community
  • 1
  • 1
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81

2 Answers2

27

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);
Thomas
  • 876
  • 6
  • 15
  • can you share sample code, how to configure from service – Yogesh Rathi Aug 05 '16 at 08:21
  • I just edited my post, this is the only configuration you need. The INTENT_FILTER will be used by your activity when you register your broadcastReceiver. – Thomas Aug 05 '16 at 08:29
  • Intent intent = new Intent(INTENT_FILTER); what is INTENT_FILTER it is constants or what ? – Yogesh Rathi Aug 05 '16 at 11:48
  • Yes it is a constant, typically you need to define somewhere in your code a public static final String INTENT_FILTER = "INTENT_FILTER" and you this constant both in your service when you create the intent and on your activity when you register your receiver – Thomas Aug 05 '16 at 11:55
  • your code is working for one activity how to configure multiple activities with same broadcasr – Yogesh Rathi Aug 05 '16 at 12:03
  • I don't think you can, or at least I don't see how. My best guess is to declare a broadcast receiver for each activity (does not seem too bad to me). – Thomas Aug 05 '16 at 12:10
  • without multiple broadcast receiver can we handle the my requirement. – Yogesh Rathi Aug 05 '16 at 12:14
  • I can't see how. Or with something that will look like a hack to me. However, if you intend to use one broadcast receiver for multiple activities, you'll have to do multiple check like if the activity is still alive and so. Otherwise you're app will crash because of NPE. – Thomas Aug 05 '16 at 13:02
  • @YogeshRathi You also can create one base activity and then inherit from it – Sirop4ik Aug 24 '17 at 13:34
-3

Try this Suppose you want to call method of MainActivity; Make static variable Context context and initialise in onCreate() of MainActivity. and make the method static which you want to call in MainActivity say

public static void updateUi()
{
// code to update Ui
} 

then call from service like this:-

((MainActivity)MainActivity.context).updateUi();

Update: if you want to perform in multiple activities then you can this method and handle the logic in this updateUi() method

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
  • this is not correct way.. then better i use static object variable.. i want to add code when my activity is running...so this is fix...and your solution may work when i start service from same activity..but i fcm service start automatically and i want to update another activity – Yogesh Rathi Aug 05 '16 at 05:48
  • @YogeshRathi Its better to analise before downvoting the answer. Can u explain what is wrong in this. For your Knowledge I want to tell FCM service is declared in `AndroidManifest.xml` you don't need to start from Activity. I am working on Firebase from last 1 year – Jagjit Singh Aug 05 '16 at 05:53
  • @YogeshRathi Can u please tell the usecase where it not worked so may i help you better way? – Jagjit Singh Aug 05 '16 at 05:59
  • i want to update GUI from 12 activity... if above code i added in all activity then it is not good programming, above requirement fullfill with help of interface may be, i am checking is it possible with help of interface – Yogesh Rathi Aug 05 '16 at 06:04
  • @YogeshRathi What are you saying?? Earlier you said code is not working now you saying this is not good practise. Please analise the things before you say. And Please mention this in your question? how would person know you want this ?? see your question it says this When my activity is running and fcm notification came then i want to update code? is there any way to handle this requirement ? Thats y I answered this way – Jagjit Singh Aug 05 '16 at 06:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120199/discussion-between-jagjit-singh-and-yogesh-rathi). – Jagjit Singh Aug 05 '16 at 06:08