Android Tutorial Firebase Realtimedatabase Create a room and messages inside

Hello,

I've been working with Firebase's realtimedatabase this week. There is no example yet of how to create rooms and send messages, but it is important for anyone who wants to make a chat in his App, for this reason I make an example of it.

I oriented myself on this example. And thanks DonManfred for his example project.

This example is maskless and purely for the Firebase structure.

The result of this exmaple/tutorial is this:
Firebase RealTimeDatabase Room and Message Structur.PNG

The room name is "Fußball" (football)
He becomes automatically a id (child)

Based on this id we can enter the room and send messages in there.

subordinate are the messages. Every message gets its own generated id.
The "senderId" is the FireBase Auth uid from the user. This is important, by which we can later assign from whom came the message. Last but not least is the "text" this is the message.

Let's get to the code:

I show only the technical behind it when you press the button, for the input is everyone responsible himself!

i use:
Firebase RealTimeDatabase lib.
FireBase AuthEx

B4X:
Sub Process_Globals
  'it is better to put it in to the starter service, not on main!
Dim auth As FirebaseAuthEx
Dim realtime As FirebaseDatabase

End Sub

Sub Globals
  
    Dim roomref As DatabaseReference
    Dim messageref As DatabaseReference

    Dim childOftheOnlyRoom as String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("frm_main")
  
    auth.Initialize("auth")
    auth.signInAnonymously 'signIn to Firebase Anonym

    realtime.Initialize("Realtime")
    realtime.PersistenceEnabled = True
    realtime.goOnline
  
    roomref.Initialize("room",realtime.getReference2("/rooms"),"room") 'is the url to the firebase database "/rooms" is the path of all rooms.
 
    roomref.addChildEventListener 'not relevant for this tutorial
    roomref.addListenerForSingleValueEvent 'this too
    roomref.keepSynced(True)
  
End Sub

Sub room_onChildAdded(Snapshot As Object, child As String, tag As Object)
childOftheOnlyRoom = child
end sub

Sub btn_createRoom_Click
'with this we are create a room with a generatet id and a customized name.   

    roomref.push.setValue(CreateMap( "name" : "The Name of the Room","newroom1", "post")
    roomref.orderByKey
   
End Sub


Sub btn_sendMessage_Click
   'here we are send a message in this room on his generatet id, we find this room_id on the onChildAdded listener as child, store this child in to your local database or on a variable.

'we need the room_id and under this id, we put "/messages" for the assignment

    messageref.Initialize("messages", realtime.getReference2("/rooms/" & childOftheOnlyRoom & "/messages") ,"message")
  
    Dim messagesitems As Map = CreateMap("senderId": auth.CurrentUser.Uid, "text": "Test Message 1")
  
   messageref.push.setValue(messagesitems,"rooms","messages")
  
End Sub

I hope you continue to help, for tips or questions I'm open.
 
Last edited:

Alan dos Santos

Member
Licensed User
Longtime User
Thank you very much for sharing this tutorial, I would like to know if you know how to fix this error that is happening to me when loading the library Firebase RealtimeDataBase.
Sorry my Google Tradutor.


"No resource found that matches the given name 'Widget.Design.TextInputLayout'. "
 

Alexander Stolte

Expert
Licensed User
Longtime User
wich version of the lib. have you? the newest?

if the newest, did you this imports on main?
B4X:
#AdditionalJar: com.google.firebase:firebase-database
#AdditionalJar: constraint-layout.aar
 

Alan dos Santos

Member
Licensed User
Longtime User
wich version of the lib. have you? the newest?

if the newest, did you this impots on main?
B4X:
#AdditionalJar: com.google.firebase:firebase-database
#AdditionalJar: constraint-layout.aar


I'm using the version of lib 2.53 and sdk api 27, and even after #AdditionalJar is still giving the same error :(
 

DonManfred

Expert
Licensed User
Longtime User
Thank you very much for sharing this tutorial, I would like to know
Posting in a TUTORIAL thread is the wrong way to ask for help!
ALWAYS (!!) create a NEW Thread in the Questions-Forum for any question you have.

You are spamming a Tutorial-Thread :-(

PD: Based on the error i would guess you need to add AppCompat Design Library to your Project.
 
Top