CHAT21是开源实时聊天平台TileDesk.com的核心。
使用CHAT21 Android SDK,您可以:
| |
|
演示应用程序源代码可在此处使用
在我们的官方教程之后,您可以建立自己的聊天
在开始之前,您需要在环境中设置一些东西:
首先,将Google-Services插件和Google的Maven存储库中的root级build.gradle文件:
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:3.1.1'
}
}
allprojects {
// ...
repositories {
// ...
google()
}
}
然后,在模块Gradle文件(通常是App/build.gradle)中,在文件底部添加应用程序插件行以启用Gradle插件:
apply plugin: 'com.android.application'
// ...
dependencies {
// ...
implementation "com.google.android.gms:play-services:11.8.0"
}
// ...
apply plugin: 'com.google.gms.google-services'
放:
将以下内容添加到您的应用/build.gradle文件:
defaultConfig {
// ...
multiDexEnabled true
}
dependencies {
// ...
compile 'com.android.support:multidex:1.0.1'
compile "com.google.android.gms:play-services:11.8.0"
compile 'com.android.support:design:26.1.0'
compile 'org.chat21.android:chat21:1.0.10'
compile 'com.vanniktech:emoji-ios:0.5.1'
}
// ...
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.1.0'
}
}
}
}
最后,如文档中所述,将此语句粘贴为文件的最后一行:
apply plugin: 'com.google.gms.google-services'
最后,您将下载一个google-services.json文件。有关更多信息,请参阅相对文档
创建自定义应用程序类
public class AppContext extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this); // add this
}
}
并将其添加到ustest.xml
<application
android:name=".AppContext"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
...
</application>
替换styles.xml中的默认父主题
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
到
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
您将获得类似的东西:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
CHAT21 SDK提供了一个CHAT.CONFIGURATION对象,该对象允许为您的聊天设置一些自定义行为和设置。
要创建CHAT21 SDK的新实例,您必须创建自己的配置(使用CHAT21 SDK CHAT.CONFIGURATION.BUILDER),然后将其用作方法Chat.initialize(configuration);
// optional
//enable persistence must be made before any other usage of FirebaseDatabase instance.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
// mandatory
// it creates the chat configurations
ChatManager.Configuration mChatConfiguration =
new ChatManager.Configuration.Builder(<APP_ID>)
.firebaseUrl(<FIREBASE_DATABASE_URL>)
.storageBucket(<STORAGE_BUCKET>)
.build();
ChatManager.start(<CONTEXT>, mChatConfiguration, <LOGGED_USER>);
代替:
<APP_ID>使用您的appid;<FIREBASE_URL>使用Firebase应用程序的Firebae数据库URL;<STORAGE_BUCKET>带有firebase应用程序的Firebae储物桶URL;<CONTEXT>在您的上下文中;<LOGGED_USER>与您当前的记录用户一起,假设它是Ichatuser的实例Chatui允许您快速将常见的UI元素连接到CHAT21 SDK API。
Chatui让您开始使用活动和内部片段聊天。
使用以下说明初始化Chatui组件
ChatUI.getInstance().setContext(this);
它启动了一个新活动,其中包含对话列表。
ChatUI.getInstance().showConversationsListActivity();
您必须创建一个内部容器的片段。
聊天将在此容器内开始,其中显示了对话列表。
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
现在,您可以使用以下方法显示聊天:
ChatUI.getInstance().openConversationsListFragment(getChildFragmentManager(), R.id.container);
com.android.support中的冲突
错误:
* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Failed to execute aapt
解决方案:在文件/project/app/build.gradle的底部复制此块
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.1.0'
}
}
}
}
多端
错误:
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
解决方案:确保您在/project/app/build.gradle的内部添加了multiDexEnabled true
在您的自定义应用程序类的内部复制此块
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
主题
错误:
RuntimeException: Unable to start activity ComponentInfo{my.sample.package.myapplication/chat21.android.ui.conversations.activities.ConversationListActivity}: java.lang.IllegalStateException:
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
解决方案:请参阅样式部分
应用程序名称例外:
错误:
/android/MyApp/app/src/main/AndroidManifest.xml:30:9 Error:
Attribute application@label value=(@string/application_name) from AndroidManifest.xml:30:9
is also present at {Library Name} value=(@string/app_name)
Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:26:5 to override
解决方案:添加tools:replace="android:label"以覆盖CHAT21 SDK APP名称和默认图标:
```
<application
android:name=".AppContext"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:label, android:icon"> <!-- add this -->
. . .
</application>
```
遵循本指南将自己的库部署到Jcenter