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.
1// Create an app event list 2// TO DO: Replace "servicesdk" with your own unique scheme 3AppEventList appEventList = new AppEventList("servicesdk"); 4 5// Add a regular expression for a path 6appEventList.addDescriptionForPath("action/settings", "Tap for regex action"); 7 8// Add a custom path for a specific path 9appEventList.addDescriptionForExpression("action\\/a.*", "Tap for path action"); -
Implement the AppLinkClickListener so that you can handle when the
user taps on the URL.
1public static class ChatAppLinkClickListener implements AppLinkClickListener { 2 3 private final Activity mActivity; 4 private final ChatUIClient mUIClient; 5 6 // Constructor requires the chat UI client and the activity 7 public ChatAppLinkClickListener(ChatUIClient uiClient, Activity activity) { 8 mActivity = activity; 9 mUIClient = uiClient; 10 } 11 12 @Override 13 public void didReceiveAppEventWithURL(@NonNull String url) { 14 15 if (url.contains("settings")) { 16 17 // Minimize chat window 18 mUIClient.minimize(); 19 20 // TO DO: Perform some action for this url. For example: 21 Intent intent = new Intent(mActivity, ChatSettingsActivity.class); 22 mActivity.startActivity(intent); 23 } 24 if (url.contains("alert")) { 25 26 // TO DO: Perform some action for this url. For example: 27 Toast.makeText( 28 mActivity.getApplicationContext(), 29 "An example of an alert", 30 Toast.LENGTH_SHORT) 31 .show(); 32 } 33 34 // NOTE: You can maximize the chat window using 35 // mUIClient.maximize(); 36 37 } 38} -
Configure the chat SDK using the ChatUIConfiguration builder. Add the AppEventList object and the AppLinkClickListener.
1new ChatUIConfiguration.Builder() 2 // … other ChatUIConfiguration details 3 .chatUIConfigurationBuilder.appEventList(appEventList) 4 .appLinkClickListener(new ChatAppLinkClickListener(context))