Android 在收到gcm推送通知时动态更新listview,而不在通知栏中显示

Android 在收到gcm推送通知时动态更新listview,而不在通知栏中显示,android,listview,google-cloud-messaging,push,Android,Listview,Google Cloud Messaging,Push,我有一个群聊功能。群中的每个人都会收到推送信息(甚至是消息的发件人)。 如果此人已打开群组,即聊天区域可见,则我希望推送不会显示在通知栏中,并直接更新聊天(我在列表视图中显示) 最初,我从web服务获得的聊天历史记录(当用户打开聊天区域时) 希望我能让你们明白我想要实现的目标。提前谢谢。我想你们应该使用Observer设计模式。 设置一个侦听器,在其中显示消息,并在服务中从服务器获取消息。如果侦听器不为空,请从侦听器调用更新方法并更新列表,否则显示通知。 此模式的示例: 我认为您应该使用Obse

我有一个群聊功能。群中的每个人都会收到推送信息(甚至是消息的发件人)。 如果此人已打开群组,即聊天区域可见,则我希望推送不会显示在通知栏中,并直接更新聊天(我在列表视图中显示)

最初,我从web服务获得的聊天历史记录(当用户打开聊天区域时)


希望我能让你们明白我想要实现的目标。提前谢谢。我想你们应该使用Observer设计模式。 设置一个侦听器,在其中显示消息,并在服务中从服务器获取消息。如果侦听器不为空,请从侦听器调用更新方法并更新列表,否则显示通知。
此模式的示例:

我认为您应该使用Observer设计模式。 设置一个侦听器,在其中显示消息,并在服务中从服务器获取消息。如果侦听器不为空,请从侦听器调用更新方法并更新列表,否则显示通知。
此模式的示例:

首先,当您在gcminentserviceonMessage()中收到消息时,发送brodcast。像

Intent i = new Intent();
                            i.setAction("appendChatScreenMsg");
                            i.putExtra("sender_id", b.getString("sender_id"));
                            i.putExtra("message", b.getString("message"));
                            i.putExtra("time", getCurrentTime());
                            i.putExtra("date", getCurrentDate());
                            this.sendBroadcast(i);
接下来,在聊天活动或聊天片段中制作BroadcastReceiver。像

BroadcastReceiver appendChatScreenMsgReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        if (b != null) {
            int totalItems = adapter.getCount() - 1;


                ChatModel model = new ChatModel("" + sharedPreferences.getString(VariableBag.USERID, ""), ""+ b.getString("sender_id"), "" + b.getString("message"), b.getString("date"), b.getString("time"));
                arrChat.add(model);


            if (adapter != null) {

                if (lstChat.getLastVisiblePosition() == totalItems) {
                    adapter.notifyDataSetChanged();
                    lstChat.setSelection(adapter.getCount());
                } else {
                    adapter.notifyDataSetChanged();
                }
            } else {
                adapter = new ChatAdapter(getActivity());
                lstChat.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                lstChat.setSelection(adapter.getCount());
            }
        }
    }
};
接下来,在onCreate()中注册BroadcastReceiver。像

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().registerReceiver(this.appendChatScreenMsgReceiver, new IntentFilter("appendChatScreenMsg"));
}
下一步,在onDestroy()中取消注册接收器。像

@Override
public void onDestroy() {
    super.onDestroy();
    getActivity().unregisterReceiver(appendChatScreenMsgReceiver);
}
说明:

1.)当GCMinentService()中收到消息时,首先检查天气 您是否在聊天屏幕中

2.)如果您在聊天屏幕中,请使用Intent和broadcast广播您的信息

3.)现在,在聊天屏幕中创建广播接收器()

4.)在onCreate()中注册BroadcastReceiver(),并在onDestroy()中注销

5.)当消息广播时,您处于聊天屏幕中,此广播接收器将收到您的捆绑包

(英译汉)现在你想做什么就做什么

7.)如果您不在聊天屏幕中,则在通知中显示受尊重的消息。不要广播


注意:确保您当前处于哪个屏幕。

首先,当您在gcminentserviceonMessage()中收到消息时,发送brodcast。像

Intent i = new Intent();
                            i.setAction("appendChatScreenMsg");
                            i.putExtra("sender_id", b.getString("sender_id"));
                            i.putExtra("message", b.getString("message"));
                            i.putExtra("time", getCurrentTime());
                            i.putExtra("date", getCurrentDate());
                            this.sendBroadcast(i);
接下来,在聊天活动或聊天片段中制作BroadcastReceiver。像

BroadcastReceiver appendChatScreenMsgReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        if (b != null) {
            int totalItems = adapter.getCount() - 1;


                ChatModel model = new ChatModel("" + sharedPreferences.getString(VariableBag.USERID, ""), ""+ b.getString("sender_id"), "" + b.getString("message"), b.getString("date"), b.getString("time"));
                arrChat.add(model);


            if (adapter != null) {

                if (lstChat.getLastVisiblePosition() == totalItems) {
                    adapter.notifyDataSetChanged();
                    lstChat.setSelection(adapter.getCount());
                } else {
                    adapter.notifyDataSetChanged();
                }
            } else {
                adapter = new ChatAdapter(getActivity());
                lstChat.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                lstChat.setSelection(adapter.getCount());
            }
        }
    }
};
接下来,在onCreate()中注册BroadcastReceiver。像

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().registerReceiver(this.appendChatScreenMsgReceiver, new IntentFilter("appendChatScreenMsg"));
}
下一步,在onDestroy()中取消注册接收器。像

@Override
public void onDestroy() {
    super.onDestroy();
    getActivity().unregisterReceiver(appendChatScreenMsgReceiver);
}
说明:

1.)当GCMinentService()中收到消息时,首先检查天气 您是否在聊天屏幕中

2.)如果您在聊天屏幕中,请使用Intent和broadcast广播您的信息

3.)现在,在聊天屏幕中创建广播接收器()

4.)在onCreate()中注册BroadcastReceiver(),并在onDestroy()中注销

5.)当消息广播时,您处于聊天屏幕中,此广播接收器将收到您的捆绑包

(英译汉)现在你想做什么就做什么

7.)如果您不在聊天屏幕中,则在通知中显示受尊重的消息。不要广播

注意:确保您当前处于哪个屏幕