在 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