During adding your application to the firebase project download the google-services.json file and copy it to the app folder inside your Android project.
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
classpath "com.android.tools.build:gradle:4.0.1"
//make a Database Reference object first
DatabaseReference databaseReference;
//following will create a sub parent in your database of name Hello
//Note:- This parent will be only visible if it has a child or value
databaseReference= FirebaseDatabase.getInstance().getReference("Hello");
//this will create a child named child1 inside Hello with the message as msg
databaseReference.child("child1").setValue("msg");
//by using push() multiple messages can be added
databaseReference.push().child("child1").setValue("msg");
//following can be used for retrieving values from the database and this will also work whenever the
//values are changed on the database
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//data is returned in the form of hashmap
//iterating the hasmap will give the desired data
HashMap hashMap = (HashMap) dataSnapshot.getValue();
System.out.println(hashMap);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Error...
}
};
databaseReference.addValueEventListener(postListener);
At first for a new user the user will be asked for a username so as to create a reference on the database.
Then a list of all users available on the database will be displayed using android ListView
Then if the users select any of the users to chat from the user list then a new database reference will be created with the name as (this username)+to+(username of the user to chat with) and this refrence will be used to send the messages, similarly there will be one more database reference with the name as (username of the user to chat with)+to+(this username) which will be used to receive messages from the other user
For dispaying chats LinearLayout is used and for storing the chat history SharedPreferences. The messages are not stored on the database, the very first time the message is received by the other user that message is deleted from the database and stored locally on phone using SharedPreferences.
- Applying Data Encryption.
- Adding Multimedia support.
- Web app to use Firebase Admin SDK for notifications of messages.
- Handling multiple users with same username.