Handling Links In Rich Content

Responsys SDK provides APIs to get the latest Message Center (MC) messages. The SDK does not enforce UI restrictions on the Message Center UX design. The UI for displaying Message Center is left for the app to implement according to its theme and colors. When a Message Center message has rich content (HTML), the most common way to display the content is by using a WebView component. WebView provides APIs to intercept all URLs being loaded (or clicked). We will use these APIs to intercept the hyperlinks. The hyperlink itself is encoded, and the actual deep link or web link can be retrieved via this hyperlink. So, we will intercept the hyperlink, make an HTTP/GET request to this URL and get back the actual link.

Implementation

  • Define a WebView component in the message details viewer screen.

    WebView content = findViewById(R.id.mc_rich_content_container);
    
    WebSettings contentWebSettings = content.getSettings();
    contentWebSettings.setJavaScriptEnabled(true);
    contentWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    contentWebSettings.setBuiltInZoomControls(false);
    contentWebSettings.setLoadWithOverviewMode(true);
    contentWebSettings.setSupportZoom(false);
    contentWebSettings.setUseWideViewPort(true);
    
    content.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    });
  • When the user clicks on any link in the message, shouldOverrideUrlLoading is called.

  • Intercept the URL here and check if it is a Responsys formatted link.

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
        if(TextUtils.isEmpty(url)){
            return false;
        }
    
        Uri uri = Uri.parse(url);
    
        if (uri == null) {
            return false;
       }
    
        final String path = uri.getPath();
    
        if (TextUtils.isEmpty(path)){
            return false;
       }
    
       boolean isResponsysLink = ("/pub/pacc".equalsIgnoreCase(path) || 
                                "/pub/pcc".equalsIgnoreCase(path));
    
        if(isResponsysLink){
            processResponsysLink(url);
            return true;
       }else{
          return false;
       }
    }
  • If the link is a Responsys link, make an HTTP/GET request.

    private void processResponsysLink(@NonNull String hyperlink){
    
        URL url;
        HttpURLConnection urlConnection;
    
        try {
            url = new URL(hyperlink);
    
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(false);
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setRequestProperty("User-Agent", "android");
    
            int responseCode = urlConnection.getResponseCode();
    
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = null;
                StringBuilder response = new StringBuilder();
                InputStream in = urlConnection.getInputStream();
    
                try {
                    reader = new BufferedReader(new InputStreamReader(in));
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                final String responseAsStr = response.toString();
    
                JSONObject responseAsJson = new JSONObject(responseAsStr);
    
                final String webLink = responseAsJson.optString("webLinkUrl");
                final String deeplink = responseAsJson.optString("mobileDeepLinkUrl");
    
                // Use the deeplink for navigating to another screen,
                // or the weblink to redirect user to a webpage.
            }
    
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
    }