This section will cover a step-by-step approach to bridge your Flutter project to PowerTalk Android SDK. We will be using Android Studio to configure native code in Flutter project.
TapTalk.io helps you to implement real-time chat with any type of your client app with speed and efficiency. Our Android SDK provides you with various methods to initialize, configure, and build the chat from the client-side - no server-side implementation is required because our reliable infra management service is delivered with the SDK. This page presents a brief overview of the SDK’s structure and abilities, then lets you go through the preliminary steps of implementing the SDK in your own app. We will be using Android Studio for the native side implementation.
Configure and Initialize TapTalk.io Android SDK
Step 1: Create a new application from your dashboard
2. Click New App Button, input App Name and choose Platform, and then click Create New App Button.
3. A pop-up dialog will be shown with provided App Key ID & App Key Secret
Note: Please remember to save your App Key ID & your App Key Secret because it will only be shown once and will be used in TapTalk.io initialization
Step 2: Install TapTalk.io SDK
To start, open the androidfolder inside your Flutter project root folder as a project using Android Studio. Then add the following repositories to your top-level build.gradle file.
After adding the required dependency, sync your project and begin the next step by writing custom platform-specific code using platform channels.
Step 3: Create Flutter Platform Client
If you have finished this step from the iOS side, you can skip to the next step.
In this example, we will add the following inside the MyHomePageState class:
Method Channel to connect to the native Android host
openPowerTalkUI() function to open the chat room list view
A button to call the openPowerTalkUI() function
main.dart
class_MyHomePageStateextendsState<MyHomePage> {// Create the MethodChannelstaticconst powerTalkMethodChannel =MethodChannel('io.taptalk.powertalk');// Function to open the chat room list UIFuture<void> _openPowerTalkUI() async {try {// Initialize PowerTalk SDKfinalbool isPowerTalkInitialized =await powerTalkMethodChannel.invokeMethod('initPowerTalk');if (isPowerTalkInitialized) {// Authenticate PowerTalk SDKfinalbool isPowerTalkAuthenticated =await powerTalkMethodChannel.invokeMethod('authPowerTalk');if (isPowerTalkAuthenticated) {// Open chat room listawait powerTalkMethodChannel.invokeMethod('openChatRoomList'); } } }onPlatformExceptioncatch (e) {print(e.message); } }@overrideWidgetbuild(BuildContext context) {returnScaffold( ... body: ...// Sample button to call openPowerTalkUIElevatedButton( onPressed: _openPowerTalkUI, child:Text('Get Started with PowerTalk', ), ), ... ); }}
Please note the name parameter in the MethodChannel constructor that was used (io.taptalk.powertalk), and the method name in the invokeMethod() parameter (initPowerTalk, authPowerTalk & openChatRoomList). We will use the same names later to configure Flutter engine in the native side.
Step 4: Add an Android platform-specific implementation
Next, we will configure the native side of the Android host implementation. Go back to previously opened Android Studio project, and open MainActivity file. We will do the following to the Activity:
Add a MethodChannel Result variable to handle asynchronous call
override configureFlutterEngine to handle calls from the MethodChannel that we previously created.
Add native functions to initialize & authenticate PowerTalk SDK and open the chat room list view
MainActivity.java
importio.taptalk.TapTalk.Helper.TapTalk;importio.taptalk.TapTalk.Listener.TapCommonListener;importio.taptalk.TapTalk.Listener.TapListener;importio.taptalk.TapTalk.Manager.TapUI;publicclassMainActivityextendsFlutterActivity {// MethodChannel Result to handle asynchronous callprivateMethodChannel.Result powerTalkMethodChannelResult =null;// Override configureFlutterEngine @OverridepublicvoidconfigureFlutterEngine(@NonNullFlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine);// Handle MethodChannel callsnewMethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),"io.taptalk.powertalk").setMethodCallHandler((call, result) -> {// Save result to variable powerTalkMethodChannelResult = result;switch (call.method) {// Call native methodscase"initPowerTalk":initPowerTalk();break;case"authPowerTalk":authPowerTalk();break;case"openChatRoomList":openChatRoomList();break;default:result.notImplemented(); } } ); }// Method to initialize PowerTalk SDKprivatevoidinitPowerTalk() {TapTalk.init(getApplicationContext(), POWERTALK_APP_KEY_ID, POWERTALK_APP_KEY_SECRET,R.drawable.your_app_icon,getString(R.string.your_app_name), POWERTALK_APP_BASE_URL, TapTalkImplementationType,newTapListener() {// You can override TapListener callbacks here } );if (powerTalkMethodChannelResult !=null) {powerTalkMethodChannelResult.success(true); } }// Method to authenticate PowerTalk SDKprivatevoidauthenticatePowerTalk() {TapTalk.authenticateWithAuthTicket( POWERTALK_AUTH_TICKET,true,newTapCommonListener() { @OverridepublicvoidonSuccess(String successMessage) {if (powerTalkMethodChannelResult !=null) {powerTalkMethodChannelResult.success(true); } } @OverridepublicvoidonError(String errorCode,String errorMessage) {if (powerTalkMethodChannelResult !=null) {powerTalkMethodChannelResult.error(errorCode, errorMessage,null); } } } ) }// Method to open chat room list viewprivatevoidopenChatRoomList() {TapUI.getInstance().openRoomList(this);if (powerTalkMethodChannelResult !=null) {powerTalkMethodChannelResult.success(true); } }}
MainActivity.kt
import io.taptalk.TapTalk.Helper.TapTalkimport io.taptalk.TapTalk.Listener.TapCommonListenerimport io.taptalk.TapTalk.Listener.TapListenerimport io.taptalk.TapTalk.Manager.TapUIclassMainActivity: FlutterActivity() {// MethodChannel Result to handle asynchronous callprivatevar powerTalkMethodChannelResult: MethodChannel.Result? =null// Override configureFlutterEngineoverridefunconfigureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)// Handle MethodChannel callsMethodChannel(flutterEngine.dartExecutor.binaryMessenger, "io.taptalk.powertalk") .setMethodCallHandler { call, result -> powerTalkMethodChannelResult = resultwhen (call.method) {"initPowerTalk"->initPowerTalk()"authPowerTalk"->authenticatePowerTalk()"openChatRoomList"->openChatRoomList()else-> result.notImplemented() } } }// Method to initialize PowerTalk SDKprivatefuninitPowerTalk() { TapTalk.init( applicationContext, POWERTALK_APP_KEY_ID, POWERTALK_APP_KEY_SECRET, R.drawable.your_app_icon,getString(R.string.your_app_name), POWERTALK_APP_BASE_URL, TapTalkImplementationType,object : TapListener() {// You can override TapListener callbacks here } ) powerTalkMethodChannelResult?.success(true) }// Method to authenticate PowerTalk SDKprivatefunauthenticatePowerTalk() { TapTalk.authenticateWithAuthTicket( POWERTALK_AUTH_TICKET,true,object : TapCommonListener() {overridefunonSuccess(successMessage: String) { powerTalkMethodChannelResult?.success(true) }overridefunonError(errorCode: String, errorMessage: String) { powerTalkMethodChannelResult?.error(errorCode, errorMessage, null) } } ) }// Method to open chat room list viewprivatefunopenChatRoomList() { TapUI.getInstance().openRoomList(this) powerTalkMethodChannelResult?.success(true) }}
You can obtain POWERTALK_AUTH_TICKET by following the guide in Authentication page.
Step 5: Enable Chat Features
Please follow the steps in Enable Chat Features page to enable TapTalk.io's chat features, such as contact sync and sending media, document, and location messages.
You can now try running the app. Pressing the sample button will invoke the openPowerTalkUI() method that we previously created, which will initialize & authenticate PowerTalk SDK, and open the chat room list view once the initialization is completed. You may also try a different implementation and separate the init, authenticate, and the open view methods. You can also add more calls to other native methods with the same approach using the same MethodChannel.
Note: You can check a more complete implementation guide in the PowerTalk Android section.