Sunday, January 8, 2017

Android Build Application with Firebase Realtime Database

In this post, I will show you How to build Application with Firebase. You can download source code here Demo Chatting.





We started. First I will guide you to integrate Firebase into your project using Android Studio.

 You click File-> Project Structure-> Clould firebase and OK as shown below:


Next. Add this following code in build.gradle android.
  1. packagingOptions {
  2. exclude 'META-INF/LICENSE'
  3. exclude 'META-INF/LICENSE-FIREBASE.txt'
  4. exclude 'META-INF/NOTICE'
  5. }

 Add firebase auth dependency. At the very bottom of the file, add apply plugin: ‘com.google.gms.google-services’

  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. testCompile 'junit:junit:4.12'
  4. compile 'com.android.support:appcompat-v7:23.3.0'
  5. compile 'com.firebase:firebase-client-android:2.3.1'
  6. compile 'com.jakewharton:butterknife:7.0.1'
  7. compile 'de.greenrobot:eventbus:2.2.+'
  8. compile 'com.google.code.gson:gson:2.3.1'
  9. }

We have the complete file: build.gradle

  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion 23
  4. buildToolsVersion "23.0.2"
  5. defaultConfig {
  6. applicationId "com.snapsofts.demofirebase"
  7. minSdkVersion 17
  8. targetSdkVersion 23
  9. versionCode 1
  10. versionName "1.0"
  11. }
  12. buildTypes {
  13. release {
  14. minifyEnabled false
  15. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  16. }
  17. }
  18. packagingOptions {
  19. exclude 'META-INF/LICENSE'
  20. exclude 'META-INF/LICENSE-FIREBASE.txt'
  21. exclude 'META-INF/NOTICE'
  22. }
  23. }
  24. dependencies {
  25. compile fileTree(dir: 'libs', include: ['*.jar'])
  26. testCompile 'junit:junit:4.12'
  27. compile 'com.android.support:appcompat-v7:23.3.0'
  28. compile 'com.firebase:firebase-client-android:2.3.1'
  29. compile 'com.jakewharton:butterknife:7.0.1'
  30. compile 'de.greenrobot:eventbus:2.2.+'
  31. compile 'com.google.code.gson:gson:2.3.1'
  32. }

In Project, I use these following libraries:

  •  Butterknife: Android Support Library Bind view and automatically cast the right type for each type of view.
  •  EventBus: Library helps transmit data between Activity - Activity, Activity - Fragment, Service - Activity.
  • Gson: Android library used to create json.
Now, we need register one account to connect Firebase. Register here




In the login and register, To be able to use function register with Firebase you need to enable this functionality in Firebase managerment as shown below.



To active and create url connect tp firebase, we use the following function:


  1. Firebase.setAndroidContext(this);
  2. Firebase myFirebaseRef = new Firebase("your_url");
Listen event when 1 value changed.
  1. private ValueEventListener valueEventListenerCurrenUser = new ValueEventListener() {
  2. @Override
  3. public void onDataChange(DataSnapshot dataSnapshot) {
  4. User user = dataSnapshot.getValue(User.class);
  5. tvUsserName.setText("Hello "+user.name);
  6. }
  7. @Override
  8. public void onCancelled(FirebaseError firebaseError) {
  9. }
  10. };


Listen event when 1 child changed.

  1. private ChildEventListener childEventListenerAllUser = new ChildEventListener() {
  2. @Override
  3. public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  4. User user = dataSnapshot.getValue(User.class);
  5. if (!dataSnapshot.getKey().equals(currenUserId)){
  6. arrStringEmail.add(user.email);
  7. arrUser.add(user);
  8. allUserAdapter.notifyDataSetChanged();
  9. }else {
  10. currenUser=user;
  11. }
  12. }
  13. @Override
  14. public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  15. if (!dataSnapshot.getKey().equals(currenUserId)){
  16. User user = dataSnapshot.getValue(User.class);
  17. int index = arrStringEmail.indexOf(user.email);
  18. arrUser.set(index, user);
  19. allUserAdapter.notifyDataSetChanged();
  20. }
  21. }
  22. @Override
  23. public void onChildRemoved(DataSnapshot dataSnapshot) {
  24. }
  25. @Override
  26. public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  27. }
  28. @Override
  29. public void onCancelled(FirebaseError firebaseError) {
  30. }
  31. };

Add code to cancel the event listened on Ondestroy function.

  1. @Override
  2. protected void onDestroy() {
  3. super.onDestroy();
  4. try {
  5. rootUrl.removeAuthStateListener(mAuthStateListener);
  6. } catch (Exception e) {
  7. }
  8. try {
  9. urlCurrenUser.removeEventListener(valueEventListenerCurrenUser);
  10. } catch (Exception e) {
  11. }
  12. try {
  13. urlAllUser.removeEventListener(childEventListenerAllUser);
  14. } catch (Exception e) {
  15. }
  16. try {
  17. rootUrl.getRoot().child(".info/connected").removeEventListener(valueEventListenerUserConnected);
  18. }catch (Exception e){}
  19. }

So, I think with some instructions above are enough to build 1 app Chatting Realtime with Firebase. Hope help you!
Share:

0 comments:

Post a Comment

Total Pageviews