使用Firebase教程的Android实时聊天应用程序

In this tutorial you will learn to build an android real time chat application using firebase database.

在本教程中,您将学习使用firebase数据库构建一个android实时聊天应用程序。

Before reading this tutorial I hope you are already familiar with basic read and write operations on firebase. If you don’t know then I would recommend you to follow below link to get basic overview about firebase.

在阅读本教程之前,我希望您已经熟悉Firebase上的基本读写操作。 如果您不知道,那么我建议您点击以下链接以获取有关Firebase的基本概述。

Read: https://firebase.google.com/docs/database/android/start/

阅读: https : //firebase.google.com/docs/database/android/start/

I have developed a very basic and simple chat app in which user can login and register in the system and can do one to one chat with other users.

我开发了一个非常简单的基本聊天应用程序,用户可以在该系统中登录和注册,并且可以与其他用户进行一对一聊天。

Android Real Time Chat Application Using Firebase Tutorial 1

影片示范 (Video Demo)

Watch below video to see how the app works.

观看下面的视频以了解该应用程序的工作原理。

使用Firebase教程的Android实时聊天应用程序 (Android Real Time Chat Application Using Firebase Tutorial)

火力基地 (Firebase)

Go to firebase console and create a new project.

转到firebase控制台并创建一个新项目。

Link: https://console.firebase.google.com/

链接: https //console.firebase.google.com/

Select Database option in left sidebar. The database structure used in this project looks as shown in below image.

在左侧边栏中选择数据库选项。 该项目中使用的数据库结构如下图所示。

使用Firebase教程2的Android实时聊天应用程序

In my case the database base url is https://android-chat-app-e711d.firebaseio.com/. It will be different in your case so make sure to change the url wherever used in the code.

在我的情况下,数据库基本URL为https://android-chat-app-e711d.firebaseio.com/ 。 您的情况将有所不同,因此请确保在代码中使用的任何地方都更改URL。

You can access the child in form of json data by just adding .json at the end of parent url. For example in this app I have accessed users data by url https://android-chat-app-e711d.firebaseio.com/users.json.

您可以通过在父URL末尾添加.json来以json数据的形式访问子级。 例如,在此应用中,我已通过url https://android-chat-app-e711d.firebaseio.com/users.json访问了用户数据。

Read more about it here https://firebase.google.com/docs/reference/rest/database/

在这里了解更多有关它的信息https://firebase.google.com/docs/reference/rest/database/

Security Rules

安全规则

By default only authenticated user can access the database. When you will go to Rules tab in firebase console project then it may look like this.

默认情况下,只有经过身份验证的用户才能访问数据库。 当您转到Firebase控制台项目中的“规则”选项卡时,它可能看起来像这样。

{
   "rules": {
     ".read": "auth != null",
     ".write": "auth != null"
   }
 }

When you try to run the app you may get authentication failure error.

当您尝试运行该应用程序时,可能会收到身份验证失败错误。

So change rules to as shown below.

因此,将规则更改为如下所示。

{
   "rules": {
     ".read": true,
     ".write": true
   }
 }

Note: Above security rules will allow anyone to read, write and edit data. That means any user can edit data of any other user. This must not be allowed. So use this security rule only for testing purpose. Don’t use it if you are making the app live in market. To learn more about firebase security rules visit below link.

注意:以上安全规则将允许任何人读取,写入和编辑数据。 这意味着任何用户都可以编辑任何其他用户的数据。 绝对不允许这样做。 因此,仅将此安全规则用于测试目的。 如果您要在市场上发布该应用程序,请不要使用它。 要了解有关Firebase安全规则的更多信息,请访问下面的链接。

https://firebase.google.com/docs/database/security/

https://firebase.google.com/docs/database/security/

Android Studio (Android Studio)

Create a new android studio project with package name com.androidchatapp

使用包名称com.androidchatapp创建一个新的android studio项目

Now add dependency for firebase database and volley in build.gradle (Module: app) file. Add following lines under dependency section and sync the project.

现在,在build.gradle(模块:app)文件中为firebase数据库和volley添加依赖项。 在“依赖项”部分下添加以下行,并同步项目。

compile 'com.firebase:firebase-client-android:2.5.2+'
 compile 'com.android.volley:volley:1.0.0'

Add internet access permission in AndroidManifest.xml file.

AndroidManifest.xml文件中添加Internet访问权限。

<uses-permission android:name="android.permission.INTERNET" />

The android studio project has following structure.

android studio项目具有以下结构。

使用Firebase教程3的Android实时聊天应用程序

Add following code in respective files.

在相应的文件中添加以下代码。

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.androidchatapp">
  
     <uses-permission android:name="android.permission.INTERNET" />
  
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <activity android:name&#
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值