Flutter Installation

Kommunicate Flutter plugin

Flutter wrapper using the native modules of Kommunicate Android and iOS SDKs.

Overview

Kommunicate Flutter SDK allows you to add customizable live chat to your applications. We support many hybrid platforms that are currently in the market; hence we have added the support for Flutter that provides a fast and expressive way for developers to build native apps on both IOS and Android.

Prerequisites

Apps using Kommunicate can target Xcode 11 or later and AndroidX is required.

Installation

  1. Add the below dependency in your pubspec.yaml file:

dependencies:
  
  kommunicate_flutter: ^1.1.3
  1. Install the package as below:

flutter pub get
  1. Import kommunicate_flutter in your .dart file to use the methods from Kommunicate:

import 'package:kommunicate_flutter/kommunicate_flutter.dart';
  1. For iOS, navigate to your App/iOS directory from terminal and run the below command:

pod install

Note: Kommunicate iOS requires min iOS platform version 10 and uses dynamic frameworks. Make sure you have the below settings at the top of your iOS/Podfile:

 platform :ios, '10.0'
 use_frameworks!

ProGuard for Android

Add the below rules in your proguard-rules.pro file under android/app/ directory. If the file is not present, create a new one and then add the rules.

#keep JSON classes                
-keep class * extends com.applozic.mobicommons.json.JsonMarker {
    !static !transient ;
}

-keepclassmembernames class * extends com.applozic.mobicommons.json.JsonParcelableMarker {
    !static !transient ;
}

#GSON Config          
-keepattributes Signature          
-keep class sun.misc.Unsafe { *; }           
-keep class com.google.gson.examples.android.model.** { *; }            
-keep class org.eclipse.paho.client.mqttv3.logging.JSR47Logger { *; } 
-keep class android.support.** { *; }
-keep interface android.support.** { *; }
-dontwarn android.support.v4.**
-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**
-keep class com.google.gson.** { *; }

Get your Application Id

Sign up for Kommunicate to get your APP_ID. This APP_ID is used to create/launch conversations.

Launch conversation

Kommunicate provides buildConversation function to create and launch conversation directly saving you the extra steps of authentication, creation, initialization and launch. You can customize the process by building the conversationObject according to your requirements. To launch the conversation you need to create a conversation object. This object is passed to the buildConversation function and based on the parameters of the object the conversation is created/launched.

Below are some examples to launch conversation in different scenarios:

Launching conversation for visitor:

If you would like to launch the conversation directly without the visiting user entering any details, then use the method as below:

    dynamic conversationObject = {
     'appId': '',
     };

    KommunicateFlutterPlugin.buildConversation(conversationObject)
        .then((clientConversationId) {
      print("Conversation builder success : " + clientConversationId.toString());
    }).catchError((error) {
      print("Conversation builder error : " + error.toString());
    });

Launching conversation for visitor with lead collection:

If you need the user to fill in details like phone number, emailId and name before starting the support chat then launch the conversation with withPreChat flag as true. In this case you wouldn't need to pass the kmUser. By this, A screen would open up for the user asking for details like emailId, phone number and name. Once the user fills the valid details (atleast emailId or phone number is required), the conversation would be launched. Use the function as below:

    dynamic conversationObject = {
         'appId': '',
         'withPreChat': true
      };

    KommunicateFlutterPlugin.buildConversation(conversationObject)
        .then((clientConversationId) {
      print("Conversation builder success : " + clientConversationId.toString());
    }).catchError((error) {
      print("Conversation builder error : " + error.toString());
    });

Launching conversation with existing user:

If you already have the user details then create a kmUser object using the details and launch the conversation. Use the method as below to create kmUser with already existing details:

   dynamic user = {
       'userId' : '',   
       'password' : ''  
     };
     dynamic conversationObject = {
         'appId': '',
         'kmUser': jsonEncode(user)
      };
      
    KommunicateFlutterPlugin.buildConversation(conversationObject)
        .then((clientConversationId) {
      print("Conversation builder success : " + clientConversationId.toString());
    }).catchError((error) {
      print("Conversation builder error : " + error.toString());
    });

Note: jsonEncode requires the dart package dart:convert. Make sure you have imported the package at the top of the dart file as import 'dart:convert';

If you have a different use-case and would like to customize the conversation creation, user creation and conversation launch, you can use more parameters in the conversationObject.

Below are all the parameters you can use to customize the conversation according to your requirements:

Parameter

Type

Description

appId

String

kmUser

kmUser

Optional, Pass the details if you have the user details, ignore otherwise. The details you pass here are used only the first time, to login the user. These login details persist until the app is uninstalled or you call logout.

withPreChat

boolean

Optional, Pass true if you would like the user to fill the details before starting the conversation. If you have user details then you can pass false or ignore.

isSingleConversation

boolean

Optional, Pass true if you would like to create only one conversation for every user. The next time user starts the conversation the same conversation would open. Pass false if you would like to create a new conversation every time the user starts the conversation. True is recommended for single-threaded conversation

metadata

dynamic

Optional. This metadata if set will be sent with all the messages sent from that device. Also, this metadata will be set to all the conversations created from that device.

agentIds

Array of String

botIds

Array of String

conversationAssignee

String

Optional, Pass either the agentId or botId. The conversation created will skip the routing rules and will be assigned to this agent or bot. You also need to pass the agentId in agentIds array or botId in the botIds array, if you are using the conversationAssignee parameter

clientConversationId

String

Optional, Pass the unique id using which you can recognize the conversation. For e.g If you have a shopping app and each order has its specific conversation, the orderId of the order can be used as a clientConversationId. In this case everyId will have its specific conversation.

conversationTitle

String

Optional, Pass the custom title for a conversation. Use this if you would like to display a custom title instead of the conversation assignee name.

createOnly

boolean

Optional, Pass true if you need to create the conversation and not launch it. In this case you will receive the clientChannelKey of the created conversation in the success callback function.

Send data to bot platform

You can set the data you want to send to the bot platform by calling the updateChatContext method as below:

  dynamic chatContext = {
          'key': 'value',
          'objKey': {
            'objKey1' : 'objValue1',
            'objKey2' : 'objValue2'
          }
        };

  KommunicateFlutterPlugin.updateChatContext(chatContext);

Logout

You can call the logout method to logout the user from Kommunicate. Use the method as below:

  KommunicateFlutterPlugin.logout();

Update user details

Use the below function to update the details of the logged in user.

    dynamic kmUser = {
       'displayName': 'New display name',
       'contactNumber': 'New contact number',
       'imageLink': 'New pofile image url',
       'email': 'New email Id',
       'metadata': {   
               'key1': 'value1',
               'key2': 'value2'
           }
     };
      
    KommunicateFlutterPlugin.updateUserDetail(kmUser)
        .then((response) {
      print("User details updated successfully : " + response.toString());
    }).catchError((error) {
      print("Error occurred while update user details : " + error.toString());
    });

Note: All the fields in the kmUser object above are optional. updateUserDetail function has to be called after conversationBuilder's success

Here is the sample app which implements this SDK: https://github.com/Kommunicate-io/Kommunicate-Flutter-Plugin/tree/master/example

Last updated