[英]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-MainFragment-聊天列表片段。
MainActivity
holds the MainFragment
which have a viewpager with tablayout. MainActivity
拥有MainFragment
,它具有带有tablayout的viewpager。 MainFragment's
view pager adapter holds ChatList
Fragment in which API call for chat list. MainFragment's
视图寻呼机适配器包含ChatList
Fragment,其中,API调用了聊天列表。
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.
但是我想更新聊天列表中的数据意味着要调用聊天列表API。
I am not getting MainActivity
context so i can access MainFragment
and retrieve that ChatList
fragment. 我没有得到
MainActivity
上下文,所以我可以访问MainFragment
并检索该ChatList
片段。
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 看看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?
您可以在此处找到示例实现: 如何使用LocalBroadcastManager?
good Question lets share my way how it works. 好问题让我分享一下它的工作方式。
in Notficationservice you nee to send broadcast 在Notficationservice中,您需要发送广播
//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 如果您不想在聊天片段时显示通知,则可以使用
onresume() & onpause()
方法管理静态标志或首选项标志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.