如何在聊天应用程序中存储上次查看的消息

时间:2017-06-03 07:30:26

标签: android firebase

我尝试使用以下代码实现群聊。

    public class mainchat extends AppCompatActivity {

    private ImageButton send;
    private FirebaseAuth mAuth;
    FirebaseDatabase database;
    DatabaseReference myRef, myref3;
    HashMap<String, String> msg;
    EditText sending;
    ListView lchat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainchat);

        //all permissions
        mAuth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();
        myref3 = database.getReference("chat");
        myref3.keepSynced(true);

        //storing
        final ArrayList<HashMap<String, String>> chatmsgs = new ArrayList<>();

        msg = new HashMap<>();
        SharedPreferences prefs = getSharedPreferences("profiledata", Context.MODE_PRIVATE);
        final String usernameee = prefs.getString("username", "username failed");

        //adapters
        final chatadapter ch = new chatadapter(this, chatmsgs,usernameee);
        lchat = (ListView) findViewById(R.id.chatlist);
        lchat.setAdapter(ch);

        //defination
        send = (ImageButton) findViewById(R.id.sendbutton1);
        sending = (EditText) findViewById(R.id.sendingtext);


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isOnline()){
                    if (sending.getText().toString().equals("")) {

                    } else {
                        msg.put("message", sending.getText().toString());
                        sending.setText("");
                        msg.put("username", usernameee);
                        msg.put("time", new SimpleDateFormat("HH:mm").format(Calendar.getInstance().getTime()));
                        myRef.child("chat").push().setValue(msg);
                    }
                }
                else{
                    Toast.makeText(mainchat.this,"please connect to internet",Toast.LENGTH_LONG).show();
                }
            }
        });

        myref3.limitToLast(15).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                HashMap<String, String> k = (HashMap<String, String>) dataSnapshot.getValue();
                chatmsgs.add(k);
                ch.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
    protected boolean isOnline()
    {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                if (activeNetwork.isConnected())
                    haveConnectedWifi = true;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                if (activeNetwork.isConnected())
                    haveConnectedMobile = true;
            }
        }

        return haveConnectedWifi || haveConnectedMobile;
    }
}
  

使用的聊天适配器类是:

    public class chatadapter extends ArrayAdapter<HashMap<String,String>> {

    private String name;
    chatadapter(Activity context, ArrayList<HashMap<String,String>> a,String name){
        super(context,0,a);
        this.name=name;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View l=convertView;
        HashMap<String,String> msgmsg=getItem(position);
        if(true){
            if(msgmsg.get("username").equals(name)){
                l = LayoutInflater.from(getContext()).inflate(R.layout.activity_rightpanel, parent, false);
            }
            else{
                l = LayoutInflater.from(getContext()).inflate(R.layout.activity_leftpanel, parent, false);
            }
        }

        TextView x=(TextView) l.findViewById(R.id.username);
        TextView y=(TextView) l.findViewById(msg);
        TextView z=(TextView) l.findViewById(R.id.timing);

        x.setText(msgmsg.get("username"));
        y.setText(msgmsg.get("message"));
        z.setText(msgmsg.get("time"));

        return l;

    }
}

现在我想向所有用户显示未读消息数的通知。并且还希望用一条线(我们已经看到消息之前)的聊天活动的意图说一些未读消息和它下面的未读消息。我怎么能实现这一点? 请帮帮我...

1 个答案:

答案 0 :(得分:1)

相反,您可以使用“共享首选项”存储上次查看的消息(ID),这样,如果新收到的消息数超过它,您可以说用小横幅或其他东西来区分未查看的消息。希望这对你有所帮助。