Hosting AE SDK in your Android app
In this guide you will learn how to integrate the AE SDK into your Android app.
The SDK supports starting a chat session with an expert and handling everything around it, including sending and receiving images and videos as well as handling notifications.
By the end of this guide you're going to have a running implementation in your app!
AE SDK for Android supports API 21 and above.
1. Add the SDK to your app
Add the SDK's Maven repository to your available repositories.
repositories {
maven {
url=uri("https://solutoltd.bintray.com/AnywhereExpertSDK")
}
}
Declare the SDK dependency - In this case we are adding version 3.2.0
.
For the latest version visit our repository at JFrog Bintray
implementation 'com.asurion.soluto:ae-sdk:3.2.0'
Make sure you have INTERNET
permission by adding the following permissions to your manifest file (should go under the manifest
tag):
<uses-permission android:name="android.permission.INTERNET"/>
At this point you should sync gradle to make sure you don't have any errors.
2. Configuring the SDK
SDK initialization
To Initialize the SDK you will need to send it the App key that you were provided with so that the SDK can fetch your app's configurations and theme.
If you don't have one, please contact the SDK team.
The app key is used by the AE platform to control different features related to your integration, for example the color scheme that will be used or which group of experts will support your users.
For the SDK to work properly you will need to call the initialize
method in the onCreate()
method of your Application class:
import com.asurion.soluto.sdk.AESDK;
public class HostingApplication extends Application {
private String appKey = "app-key-provided-for-you";
@Override
public void onCreate() {
super.onCreate();
AESDK.initialize(this, appKey);
}
}
import com.asurion.soluto.sdk.AESDK
class HostingApplication : Application() {
private val appKey = "app-key-provided-for-you"
override fun onCreate() {
super.onCreate()
AESDK.initialize(this, appKey)
}
}
initialize
method parameters
Name | Type | Description | Default |
---|---|---|---|
context (required) | Context | --- | |
appKey (required) | String | Your unique SDK App Key | --- |
Authenticating users
You can read about the options you have to authenticate users in here.
Authenticating users using JWT Token
signIn method can be called multiple times without any concern and can also be called before showing the chat view.
AESDK.signIn(authenticationToken, new UserMetadata("name"););
AESDK.signIn(authenticationToken, UserMetadata("name"))
signIn
method parameters
Name | Type | Description | Default |
---|---|---|---|
authenticationToken (required) | String | Your JWT token | --- |
userMetadata | UserMetadata | Defines the connected user - relevant only for Family Chat | null |
Events from the Sdk
You can get notified on events raising from the SDK, such as customer actions, expert actions, or technical events such as login events.
A complete list of the available events can be found here.
Registering to events can be done only BEFORE you call `initialize` method.
AESDK.registerEvents(
new String[]{"MESSAGE_SENT", "MESSAGE_RECEIVED", "ACTION", "IMAGE_PREVIEW"},
new IEventsHandler() {
@Override
public void onEventEmitted(String eventName, Map<String, Object> args) {
Log.i("AESDK", String.format("Event raised: $s", eventName));
}
}
);
AESDK.registerEvents(
arrayOf<String>("MESSAGE_SENT", "MESSAGE_RECEIVED", "ACTION", "IMAGE_PREVIEW")) {
eventName, args -> Log.i("AESDK", "Event raised: $eventName")
}
registerEvents
method parameters
Name | Type | Description | Default |
---|---|---|---|
eventsNames (required) | String[] | List of events to register to | --- |
handler (required) | IEventsHandler | Handler that will be called when an event raised | --- |
3. Enabling push notifications
The Anywhere Expert platform supports sending push notifications using FCM.
To allow your application to receive notifications you will need to set up Firebase Cloud Messaging (FCM) in your app.
In case you haven't already set up FCM in your app you can follow Firebase instructions.
For the SDK to send notifications to your app you will need to:
- Send the SDK team your Firebase Cloud Messaging
Server Key
which can be found in the settings of the project (under Cloud Messaging tab). - Supply a push notification token to the SDK.
- Handle push notifictions.
Supply push notification token
Supplying the push notification token to the SDK is done by calling setPushNotificationToken
.
There are 2 places where you need to supply the token to the SDK:
- After initialization by retriving your current registration token.
- When a new FCM token was generated.
Sending the SDK your current registration token
We encourage you to supply the FCM token as soon as possible.
The following example is based on Firebase's example on how to retrive the current registration token.
import com.asurion.soluto.sdk.AESDK;
public class HostingApplication extends Application {
private String appKey = "app-key-provided-for-you";
@Override
public void onCreate() {
super.onCreate();
AESDK.initialize(this, appKey);
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
AESDK.setPushNotificationToken(instanceIdResult.getToken());
}
});
}
}
import com.asurion.soluto.sdk.AESDK
class HostingApplication : Application() {
private val appKey = "app-key-provided-for-you"
override fun onCreate() {
super.onCreate()
AESDK.initialize(this, appKey)
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
AESDK.setPushNotificationToken(it.token)
}
}
}
New FCM token was generated
Based on Firebase's example on how to monitor token generation
public class NotificationMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String token) {
super.onNewToken(token);
// New token arrived from Firebase
AESDK.setPushNotificationToken(token);
}
}
class NotificationMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
// New token arrived from Firebase
AESDK.setPushNotificationToken(token)
}
}
setPushNotificationToken
method parameters
Name | Type | Description | Default |
---|---|---|---|
token (required) | String | FCM token used to send notifications | --- |
Handle push notifications
Calling handleNotification before calling signIn will result in an error.
The app will receive a push notification whenever the service sends a message to the user.
The SDK can handle these notifications by popping up the chat view and showing the latest message.
The notification will be displayed in 2 cases:
- The hosting app is in the background.
- The hosting app is open but the chat activity is not on top.
To enable this behavior you need to add the following code to your notifications handlers.
public class NotificationMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
AESDK.signIn('your-JWT-token');
// Forward push notifications to the SDK so it will be able to handle it.
if (!AESDK.handleNotification(remoteMessage))
{
// The notification is not related to the SDK - handle it yourself
}
}
}
class NotificationMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
// getAuthenticationToken is just an example for a function
// (not implemented in this example) that returns the authentication token of the app
AESDK.signIn(getAuthenticationToken())
// Forward push notifications to the SDK so it will be able to handle it.
if (!AESDK.handleNotification(message))
{
// The notification is not related to the SDK - handle it yourself
}
}
}
handleNotification
method parameters
Name | Type | Description | Default |
---|---|---|---|
message (required) | RemoteMessage | a remote Firebase Message | --- |
stackBuilder | TaskstackBuilder | see Create a custom back stack for notifications | null |
Create a custom back stack for notifications
When the user clicks the back
button from the chat view he will see the last open activity.
In case the app wasn't active when the user opened the SDK notification, the back
button will close the app (since there's no "last" activity).
You can control which activity the user will see in case he clicks the back
button by supplying the handleNotification
method a TaskStackBuilder
(you can read more about TaskStackBuilder).
4. Starting a chat session
Calling showChatView before calling signIn will result in an error.
All you have to do is call a single method:
AESDK.showChatView();
AESDK.showChatView()
5. Testing the app
At this point you should be able to open a chat session.
The result will look similar to:
To test the SDK you can chat with our bot by typing ~simbot text
. As a result the bot with answer with automated messages.
Associated Types
UserMetadata
Parameters
Name | Type | Description | Default |
---|---|---|---|
name (required) | String | the user's display name (will appear as part of the message) | --- |
imageUrl | String | Users avatar | null |
groupId | String | The id of the group the user is part of | null |
IEventsHandler
Interface used to describe the function that will be called when a new event raised from the SDK
public interface IEventsHandler {
void onEventEmitted(String eventName, Map<String, Object> args);
}
Parameters
Name | Type | Description | Default |
---|---|---|---|
eventName | String | The name of the raised event | --- |
args | Map<String,Object> | List of arguments that are relevant to the raising event | Empty Map<String,Object> |