0

This is not a duplicate question. So please don't make this question as a duplicate.

I have scenario. I am implementing chat module in which all functionalities i have already done.

MainActivity - MainFragment - Chat List Fragment.

MainActivity holds the MainFragment which have a viewpager with tablayout. MainFragment's view pager adapter holds ChatList Fragment in which API call for chat list.

Now if i already in chat list fragment and push notification come for message then notification should not be appeared that i did. But i want to update data in chat list means want to call chat list API.

I am not getting MainActivity context so i can access MainFragment and retrieve that ChatList fragment.

Code

 case "chat_message":




//                if (HirerTabsActivity.isOpenChat()) {
//                    Log.v("ATATATA", "asd");
//
//                } else {

                if (GetUserProfileData.getInstance().getUserDetail().getUser_type().equalsIgnoreCase("artist")) {
                    intent = new Intent(this, ArtistTabsActivity.class);
                    intent.putExtra("fromNotification", "Y");

                } else {
                    intent = new Intent(this, HirerTabsActivity.class);
                    intent.putExtra("fromNotification", "Y");
                }


            break;

Updated Code

case "chat_message":

            if (mediaPrefs.getString(Constant.SharedPreferences_IN_CHAT_SCREEN, "").equalsIgnoreCase("Y")) {
                sendBroadcast(new Intent().setAction("chat_refresh"));
            } else {

                if (GetUserProfileData.getInstance().getUserDetail().getUser_type().equalsIgnoreCase("artist")) {
                    intent = new Intent(this, ArtistTabsActivity.class);
                    intent.putExtra("fromNotification", "Y");

                } else {
                    intent = new Intent(this, HirerTabsActivity.class);
                    intent.putExtra("fromNotification", "Y");
                }
            }

Advanced help would be appreciated!

2 Answers2

0

Have a look at LocalBroadcastManager: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager

With this you can send a local broadcast from your Service listening for pushes to your Activity/Fragment. You can find an example implementation here: How to use LocalBroadcastManager?

Allinone51
  • 624
  • 1
  • 7
  • 19
  • But i want to know that I am in current fragment. –  Jul 23 '19 at 10:50
  • You can use this to see if your fragment is visible or not: https://stackoverflow.com/questions/16652095/how-to-know-if-a-fragment-is-visible/16653927 Do this in your broadcastreceiver in your fragment – Allinone51 Jul 23 '19 at 10:52
0

good Question lets share my way how it works.

in Notficationservice you nee to send broadcast

 //refresh chat list 
 sendBroadcast(new Intent().setAction("chat_refresh"));

and at your fragment

 private class RefreshBrodcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (getUserVisibleHint() && isVisible()) {
               // your can refresh via api
            }
        }
    }

If you don't want to show a notification at when you are on chat fragment then you can manage Static flag or Preference flag with onresume() & onpause() method

Vishal Patel
  • 2,931
  • 3
  • 24
  • 60
  • Okay. But suppose if i m not in chat screen then ?? –  Jul 23 '19 at 10:51
  • if your flag false at your notification service class put if else condition i just edit last two line please check @PiyushGupta – Vishal Patel Jul 23 '19 at 10:54
  • @PiyushGupta at top of Notificationservice class you just put a condition that if flag true (either from preference or static variable) then show noti otherwise fire broadcast thats very simple way if you understand – Vishal Patel Jul 23 '19 at 10:57
  • Check my updated code again. In onResume set Flag "Y" and in onPause set "N" in CHat list fragment.@Vishal Patel –  Jul 23 '19 at 10:57
  • For that you have to understand Broadcast manager properly + have to debug that flow. – Vishal Patel Jul 23 '19 at 12:51
  • I have debug that too though not working. Also used Event Bus but i m not getting that too. –  Jul 23 '19 at 12:53