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 " />
이 라이브러리를 사용할 수 있는 몇 가지 방법이 있습니다. 모든 기능을 사용하려면 활동에 보기를 추가하는 정적 메서드를 사용하거나 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를 숨길 수 있습니다. 뷰가 표시되면 숨기고 true를 반환하는 onBackPressed() 함수를 사용할 수 있습니다. 활동/프래그먼트에 다음을 추가합니다.
@ Override
public boolean onBackPressed () {
return fullScreenChatWindow != null && fullScreenChatWindow . onBackPressed ();
}
이 청취자는 다음과 같은 기회를 제공합니다.
사용자에게 파일 전송 기능을 제공하려면 ChatWindowView
에서 supportFileSharing
통해 설정해야 합니다. 운영 체제가 파일 선택 의도를 처리할 수 없는 경우 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
사용하는 것이 좋습니다.
새 활동에서 채팅 창을 열려면 매니페스트에서 ChatWindowActivity
선언해야 합니다. <application></application>
태그 사이에 AndroidManifest.xml
에 다음 줄을 추가합니다.
< activity
android : name = " com.livechatinc.inappchat.ChatWindowActivity "
android : configChanges = " orientation|screenSize "
android : exported = " false "
/>
마지막으로 애플리케이션의 채팅 창을 열려는 위치(예: 버튼 리스너)에 다음 코드를 추가합니다. 컨텍스트(활동 또는 애플리케이션 개체)와 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 );
방문자의 이름과 이메일을 제공하고 채팅 전 설문조사를 비활성화하여 채팅 창에 자동으로 로그인하는 것도 가능합니다.
새 조각에서 채팅 창을 열려면 애플리케이션의 채팅 창을 열려는 위치(예: 버튼 리스너)에 다음 코드를 추가해야 합니다. 또한 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 통합에는 AUDIO 및 VIDEO 권한이 필요합니다. 사용자가 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
확인하세요.