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.
- packagingOptions {
- exclude 'META-INF/LICENSE'
- exclude 'META-INF/LICENSE-FIREBASE.txt'
- exclude 'META-INF/NOTICE'
- }
Add firebase auth dependency. At the very bottom of the file, add apply plugin: ‘com.google.gms.google-services’
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- testCompile 'junit:junit:4.12'
- compile 'com.android.support:appcompat-v7:23.3.0'
- compile 'com.firebase:firebase-client-android:2.3.1'
- compile 'com.jakewharton:butterknife:7.0.1'
- compile 'de.greenrobot:eventbus:2.2.+'
- compile 'com.google.code.gson:gson:2.3.1'
- }
We have the complete file: build.gradle
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 23
- buildToolsVersion "23.0.2"
- defaultConfig {
- applicationId "com.snapsofts.demofirebase"
- minSdkVersion 17
- targetSdkVersion 23
- versionCode 1
- versionName "1.0"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- packagingOptions {
- exclude 'META-INF/LICENSE'
- exclude 'META-INF/LICENSE-FIREBASE.txt'
- exclude 'META-INF/NOTICE'
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- testCompile 'junit:junit:4.12'
- compile 'com.android.support:appcompat-v7:23.3.0'
- compile 'com.firebase:firebase-client-android:2.3.1'
- compile 'com.jakewharton:butterknife:7.0.1'
- compile 'de.greenrobot:eventbus:2.2.+'
- compile 'com.google.code.gson:gson:2.3.1'
- }
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:
- Firebase.setAndroidContext(this);
- Firebase myFirebaseRef = new Firebase("your_url");
Listen event when 1 value changed.
- private ValueEventListener valueEventListenerCurrenUser = new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- User user = dataSnapshot.getValue(User.class);
- tvUsserName.setText("Hello "+user.name);
- }
- @Override
- public void onCancelled(FirebaseError firebaseError) {
- }
- };
Listen event when 1 child changed.
- private ChildEventListener childEventListenerAllUser = new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot dataSnapshot, String s) {
- User user = dataSnapshot.getValue(User.class);
- if (!dataSnapshot.getKey().equals(currenUserId)){
- arrStringEmail.add(user.email);
- arrUser.add(user);
- allUserAdapter.notifyDataSetChanged();
- }else {
- currenUser=user;
- }
- }
- @Override
- public void onChildChanged(DataSnapshot dataSnapshot, String s) {
- if (!dataSnapshot.getKey().equals(currenUserId)){
- User user = dataSnapshot.getValue(User.class);
- int index = arrStringEmail.indexOf(user.email);
- arrUser.set(index, user);
- allUserAdapter.notifyDataSetChanged();
- }
- }
- @Override
- public void onChildRemoved(DataSnapshot dataSnapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot dataSnapshot, String s) {
- }
- @Override
- public void onCancelled(FirebaseError firebaseError) {
- }
- };
Add code to cancel the event listened on Ondestroy function.
- @Override
- protected void onDestroy() {
- super.onDestroy();
- try {
- rootUrl.removeAuthStateListener(mAuthStateListener);
- } catch (Exception e) {
- }
- try {
- urlCurrenUser.removeEventListener(valueEventListenerCurrenUser);
- } catch (Exception e) {
- }
- try {
- urlAllUser.removeEventListener(childEventListenerAllUser);
- } catch (Exception e) {
- }
- try {
- rootUrl.getRoot().child(".info/connected").removeEventListener(valueEventListenerUserConnected);
- }catch (Exception e){}
- }
So, I think with some instructions above are enough to build 1 app Chatting Realtime with Firebase. Hope help you!
0 comments:
Post a Comment