Handle Custom URLs in Chat
When an agent sends a standard link to a user in your app, a preview tile appears that the user can tap to view in a browser. However, you can come up with your own URL scheme that displays a custom tile and performs a custom action within your app.
-
Create a custom URL scheme and expression.
Create a URL scheme using the AppEventList class. This class allows you to add URLs either for specific paths (such as action/settings) or using regular expressions (such as action\\/a.*). Patterns are matched in the order that you add them to the AppEventList object. Be sure to add items starting with the most specific and ending with the most generic.
// Create an app event list // TO DO: Replace "servicesdk" with your own unique scheme AppEventList appEventList = new AppEventList("servicesdk"); // Add a regular expression for a path appEventList.addDescriptionForPath("action/settings", "Tap for regex action"); // Add a custom path for a specific path appEventList.addDescriptionForExpression("action\\/a.*", "Tap for path action");
-
Implement the AppLinkClickListener so that you can handle when the user taps on the URL.
public static class ChatAppLinkClickListener implements AppLinkClickListener { private final Activity mActivity; private final ChatUIClient mUIClient; // Constructor requires the chat UI client and the activity public ChatAppLinkClickListener(ChatUIClient uiClient, Activity activity) { mActivity = activity; mUIClient = uiClient; } @Override public void didReceiveAppEventWithURL(@NonNull String url) { if (url.contains("settings")) { // Minimize chat window mUIClient.minimize(); // TO DO: Perform some action for this url. For example: Intent intent = new Intent(mActivity, ChatSettingsActivity.class); mActivity.startActivity(intent); } if (url.contains("alert")) { // TO DO: Perform some action for this url. For example: Toast.makeText( mActivity.getApplicationContext(), "An example of an alert", Toast.LENGTH_SHORT) .show(); } // NOTE: You can maximize the chat window using // mUIClient.maximize(); } }
-
Configure the chat SDK using the ChatUIConfiguration builder. Add the AppEventList object and the AppLinkClickListener.
new ChatUIConfiguration.Builder() // … other ChatUIConfiguration details .chatUIConfigurationBuilder.appEventList(appEventList) .appLinkClickListener(new ChatAppLinkClickListener(context))