How can I initialize a Firebase Libary?

I want to save a chat message to the Firebase real-time database.

Before that, the Firebase Libary has to be initialized. How does this work? The code Firebase.AndroitSetContext (this); is unfortunately out of date.

The subsequent code for the creation is as follows:

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String messageText = messageArea.getText().toString();

                if(!messageText.equals("")){
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("message", messageText);
                    map.put("user", UserDetails.username);
                    reference1.push().setValue(map);
                    reference2.push().setValue(map);
                    messageArea.setText("");
                }
            }
        });

        reference1.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Map map = dataSnapshot.getValue(Map.class);
                String message = map.get("message").toString();
                String userName = map.get("user").toString();

                if(userName.equals(UserDetails.username)){
                    addMessageBox("You:-\n" + message, 1);
                }
                else{
                    addMessageBox(UserDetails.chatWith + ":-\n" + message, 2);
                }
            }

            @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(FirebaseError firebaseError) {

            }
        });
    }

The following example does not work:

Firebase.setAndroidContext(this);
        reference1 = new Firebase("https://androidchatapp-76776.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
        reference2 = new Firebase("https://androidchatapp-76776.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);

Can someone help me?

1 answer

  • answered 2020-01-18 15:25 Frank van Puffelen

    The new Firebase(...) and Firebase.AndroitSetContext (this) point to a very old version of the Firebase SDK (released before May 2016). I highly recommend finding a more up-to-date tutorial, or starting from Firebase's own documentation, or codelab, which (conveniently) also deals with building a chat app.

    Some specifics:

    • Calling setAndroidContext() is no longer needed, as it's automatically determine by the library (or other API calls) where needed.
    • The call to new Firebase(...) looks like FirebaseDatabase.getInstance().getReference() these days. The URL is no longer needed, as it's automatically extracted from the google-services.json file during build time.

    But as said, I highly recommend starting from a more up-to-date tutorial or documentation.