在 Android 应用程序中嵌入移动聊天窗口
实时聊天: https://developers.livechat.com/docs/getting-started/installing-livechat/android-widget/
要将项目添加到您的构建中:
步骤 1. 将 JitPack 存储库添加到构建文件中 将其添加到存储库末尾的根 build.gradle 中:
allprojects {
repositories {
.. .
maven { url ' https://jitpack.io ' }
}
}
步骤2.添加依赖
dependencies {
implementation 'com.github.livechat:chat-window-android:v2.4.3'
}
您的应用程序需要获得使用互联网的许可。将以下行添加到您的AndroidManifest.xml中:
< uses-permission android : name = " android.permission.INTERNET " />
您可以通过多种方式使用该库。为了能够使用所有功能,我们建议您将聊天窗口添加为视图,方法是使用将视图添加到 Activity 的静态方法,或者作为 xml 中的嵌入视图。只要ChatWindowView被初始化,当有新消息进来时你就会收到事件。
首先,您需要配置聊天窗口
只需使用 ChatWindowConfiguration.java 构造函数即可。请注意,许可证号是强制性的。
configuration = new ChatWindowConfiguration (
"your_licence_number" ,
"group_id" , // optional
"Visitor name" , // optional
"[email protected]" , // optional
customParamsMap // optional
);
您还可以使用new ChatWindowConfiguration.Builder()
。
有两种推荐的使用 ChatWindow 的方法。
您所需要做的就是创建、附加和初始化聊天窗口。内容如下:
public void startFullScreenChat () {
if ( fullScreenChatWindow == null ) {
fullScreenChatWindow = ChatWindowUtils . createAndAttachChatWindowInstance ( getActivity ());
fullScreenChatWindow . setEventsListener ( this );
fullScreenChatWindow . init ( configuration );
}
fullScreenChatWindow . showChatWindow ();
}
如果您想控制 ChatWindowView 的位置和大小,您可能需要通过在 XML 中添加视图来将其添加到您的应用程序中
< com .livechatinc.inappchat.ChatWindowViewImpl
android : id = " @+id/embedded_chat_window "
android : layout_width = " match_parent "
android : layout_height = " 400dp " />
或者直接膨胀视图
ChatWindowViewImpl chatWindowView = new ChatWindowViewImpl ( MainActivity . this );
然后像全屏窗口方法一样初始化 ChatWindow:
public void startEmmbeddedChat ( View view ) {
emmbeddedChatWindow . setEventsListener ( this );
emmbeddedChatWindow . init ( configuration );
// ...
emmbeddedChatWindow . showChatWindow ();
}
根据您的用例,如果用户点击后退按钮,您可能希望隐藏 ChatWindow。您可以使用我们的 onBackPressed() 函数,该函数会隐藏视图(如果视图可见)并返回 true。在您的活动/片段中添加以下内容:
@ Override
public boolean onBackPressed () {
return fullScreenChatWindow != null && fullScreenChatWindow . onBackPressed ();
}
这位听众让您有机会:
为了向用户提供发送文件的功能,您需要通过ChatWindowView
上的supportFileSharing
进行设置。如果操作系统无法处理选取文件的 Intent,您可以通过ChatWindowEventsListener
中的onFilePickerActivityNotFound
回调来处理。
当用户选择链接时,您可以通过实现 ChatWindowEventsListener 中的handleUri
方法来禁用聊天小部件的默认行为。
@ Override
public boolean handleUri ( Uri uri ) {
// Handle uri here...
return true ; // Return true to disable default behavior.
}
您可能希望在遇到错误(例如互联网连接问题)时自定义用户体验。通过在onError
回调方法中返回true
,您将负责处理来自聊天窗口的错误。
请记住,聊天窗口一旦加载,就可以通过偶尔尝试重新连接来处理连接问题。这种情况可以通过在 onError 回调方法中实现以下条件来检测。
@ Override
public boolean onError ( ChatWindowErrorType errorType , int errorCode , String errorDescription ) {
if ( errorType == ChatWindowErrorType . WebViewClient && errorCode == - 2 && chatWindow . isChatLoaded ()) {
//Chat window can handle reconnection. You might want to delegate this to chat window
return false ;
} else {
reloadChatBtn . setVisibility ( View . VISIBLE );
}
Toast . makeText ( getActivity (), errorDescription , Toast . LENGTH_SHORT ). show ();
return true ;
}
用户退出应用程序后,您可能需要清除聊天会话。您可以通过从应用程序中的任何位置调用ChatWindowUtils.clearSession()
上的静态方法来完成此操作。如果您的ChatWindowView
在注销流程中附加,您还需要在clearSession 代码之后调用chatWindow.reload()
来重新加载它。请参阅 FullScreenWindowActivityExample.java
如果您不需要在用户在隐藏的 ChatWindow 中收到新消息时收到通知,您可能需要使用ChatWindowActivity
或ChatWindowFragment
为了在新的 Activity 中打开聊天窗口,您需要在清单中声明ChatWindowActivity
。将以下行添加到AndroidManifest.xml
的<application></application>
标记之间:
< activity
android : name = " com.livechatinc.inappchat.ChatWindowActivity "
android : configChanges = " orientation|screenSize "
android : exported = " false "
/>
最后,将以下代码添加到您的应用程序中要打开聊天窗口的位置(例如按钮侦听器)。您需要提供上下文(您的 Activity 或应用程序对象)和您的 LiveChat 许可证号:
Intent intent = new Intent ( context , com . livechatinc . inappchat . ChatWindowActivity . class );
Bundle config = new ChatWindowConfiguration . Builder ()
. setLicenceNumber ( "<your_license_number>" )
. build ();
intent . putExtra ( ChatWindowConfiguration . KEY_CHAT_WINDOW_CONFIG , windowConfig );
startActivity ( intent );
还可以通过提供访客的姓名和电子邮件并禁用聊天前调查来自动登录聊天窗口。
为了在新的 Fragment 中打开聊天窗口,您需要将以下代码添加到应用程序中要打开聊天窗口的位置(例如按钮侦听器)。您还需要提供您的 LiveChat 许可证号:
getSupportFragmentManager ()
. beginTransaction ()
. replace (
R . id . frame_layout ,
ChatWindowFragment . newInstance (
"your_license_number" ,
"your_group_id" , // optional
"visitor_name" , // optional
"visitor_email" , // optional
),
"chat_fragment"
)
. addToBackStack ( "chat_fragment" )
. commit ();
还可以通过向 Fragment 提供访问者的姓名和电子邮件并禁用聊天前调查来自动登录聊天窗口。
您可以通过使用以下 id 定义您自己的字符串资源来更改或本地化错误消息
< string name = " failed_to_load_chat " >Couldn't load chat.</ string >
< string name = " cant_share_files " >File sharing is not configured for this app</ string >
< string name = " reload_chat " >Reload</ string >
从版本 2.4.0 开始,CHANGELOG.md 中列出了迁移详细信息。
setUpWindow(configuration);
被setConfiguration(configuration);
setUpListener(listener)
替换为setEventsListener(listener)
ChatWindowView.clearSession()
移至ChatWindowUtils.clearSession(Context)
ChatWindowView.createAndAttachChatWindowInstance(Activity)
移至ChatWindowUtils.createAndAttachChatWindowInstance(getActivity())
android.permission.READ_EXTERNAL_STORAGE
权限SnapCall 集成需要音频和视频权限。为了允许您的用户使用 SnapCall 集成,您需要:
AndroidManifest.xml
文件 < uses-permission android : name = " android.permission.RECORD_AUDIO " />
< uses-permission android : name = " android.permission.MODIFY_AUDIO_SETTINGS " />
< uses-permission android : name = " android.permission.CAMERA " />
void onRequestAudioPermissions(String[] permissions, int requestCode)
向用户请求权限,如下所示: @ Override
public void onRequestAudioPermissions ( String [] permissions , int requestCode ) {
if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . M ) {
this . requestPermissions ( permissions , requestCode );
}
}
void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
将结果传递给ChatWindowView
if (! chatWindow . onRequestPermissionsResult ( requestCode , permissions , grantResults )) {
super . onRequestPermissionsResult ( requestCode , permissions , grantResults );
}
作为参考,请检查FullScreenWindowActivityExample.java