简体   繁体   中英

Update data in fragment with getting push notification

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!

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM