Calling Activity method from FirebaseMessagingService

krish

I'm trying to create an android app which receives Firebase message from server and changes the source image of a ImageView by calling a function.

my firebase Service:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String body = remoteMessage.getNotification().getBody();
    MainActivity.changeUi(body);
}

My MainActivity:

public class MainActivity extends AppCompatActivity {
static ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Loading Home Layout
    setContentView(R.layout.activity_home);
    iv=findViewById(R.id.imgView);
}
 public static void changeUi(String s){
    if(s=="change")
      iv.setImageResource(R.drawable.image2);
 }
}

The App Crashes on sending firebase message. This is my first app and I could not understand other answers. Thanks for the help.

Shabbir Dhangot

Calling static method will create new instance of that activity. Here its crashing due to imageView not initialize.

You can use BroadCast Receiver here. Below is an example.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String body = remoteMessage.getNotification().getBody();
    Intent myIntent = new Intent("FBR-IMAGE");
    myIntent.putExtra("action",body);
    this.sendBroadcast(myIntent);
}

On Activity you need to create and register receiver

public BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getStringExtra("action");
        changeUi(action);
    }
};

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(myReceiver, new IntentFilter("FBR-IMAGE"));
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(myReceiver);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method from Activity in Adapter

From Dev

Calling a method from an exported activity

From Dev

Calling method of activity from asynctask

From Dev

Calling a activity method from a BroadCast Receiver

From Dev

NullPointerException when calling a Fragment method from Activity?

From

Calling a Fragment method from a parent Activity

From

Calling activity class method from Service class

From Dev

calling parent activity method from fragment

From Dev

NullPointer when calling Fragment method from activity

From Dev

Calling a method inside Activity from BroadcastReceiver

From Dev

Calling fragment method from activity Android

From Dev

calling an activity method from custom onClickListener class

From Dev

Calling method from another activity crashes

From Dev

How to find out the current (top) Activity from onMessageReceived (of FirebaseMessagingService)?

From Java

How to solve error with calling kotlin activity method from java singleton?

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

Calling Fragment's method from Activity doesn't work?

From Dev

How a static method get context from the calling activity?

From Dev

Calling Activity from Fragment

From Dev

calling BroadcastReceiver from Activity

From Dev

Calling fragment from activity

From Dev

Calling Main activity method in broadcastReceiver

From Dev

Calling Activity method inside Fragment that is not attached to Activity

From Dev

Calling activity from class that doesnt extend Activity

From Dev

calling an activity from fragment activity through Intent

From Dev

Calling a custom fragment from an activity

From Dev

Calling Activity from Async Task

From Java

Calling activity from ViewHolder in RecyclerView?

From Dev

Calling startActivity() from outside of an Activity

Related Related

  1. 1

    Calling a method from Activity in Adapter

  2. 2

    Calling a method from an exported activity

  3. 3

    Calling method of activity from asynctask

  4. 4

    Calling a activity method from a BroadCast Receiver

  5. 5

    NullPointerException when calling a Fragment method from Activity?

  6. 6

    Calling a Fragment method from a parent Activity

  7. 7

    Calling activity class method from Service class

  8. 8

    calling parent activity method from fragment

  9. 9

    NullPointer when calling Fragment method from activity

  10. 10

    Calling a method inside Activity from BroadcastReceiver

  11. 11

    Calling fragment method from activity Android

  12. 12

    calling an activity method from custom onClickListener class

  13. 13

    Calling method from another activity crashes

  14. 14

    How to find out the current (top) Activity from onMessageReceived (of FirebaseMessagingService)?

  15. 15

    How to solve error with calling kotlin activity method from java singleton?

  16. 16

    Calling Fragment method from Activity,findFragmentById returns null

  17. 17

    Calling Fragment's method from Activity doesn't work?

  18. 18

    How a static method get context from the calling activity?

  19. 19

    Calling Activity from Fragment

  20. 20

    calling BroadcastReceiver from Activity

  21. 21

    Calling fragment from activity

  22. 22

    Calling Main activity method in broadcastReceiver

  23. 23

    Calling Activity method inside Fragment that is not attached to Activity

  24. 24

    Calling activity from class that doesnt extend Activity

  25. 25

    calling an activity from fragment activity through Intent

  26. 26

    Calling a custom fragment from an activity

  27. 27

    Calling Activity from Async Task

  28. 28

    Calling activity from ViewHolder in RecyclerView?

  29. 29

    Calling startActivity() from outside of an Activity

HotTag

Archive