Prati
Prati

Reputation: 283

How to integrate Disqus in my Android App?

I am developing an android app for a web-site. The web-site supports Disqus for commenting service. I want to support the same in my application. I got APIs from Disqus docs but still I am not clear how to integrate them in my application. Please help me understand the implementation. Is anyone integrated Disqus comment service into their Android App?

Upvotes: 6

Views: 7455

Answers (3)

Emirhan Eyidoğan
Emirhan Eyidoğan

Reputation: 11

You can use this code : Google Login is working. I did not tested Facebook yet.

static void setupDisqus(Context context, WebView disqus) {
    try {
        String URL = ""; // URL must be unique like identifier! Because Disqus, is using the url instead of identifier.
        String identifier = "";
        String shortName = "";
        String commentsUri = "https://captainsp.github.io/disqus_comments_dark_gray.html?" + "shortname=" + shortName +
                "&url=" + URLEncoder.encode(URL, "UTF-8") +
                "&title=" + URLEncoder.encode("Comments", "UTF-8") +
                "&identifier=" + URLEncoder.encode(identifier, "UTF-8");

        /*
         * You can use this colors in my Github Account:
         * disqus_comments_dark_gray.html
         * disqus_comments.html
         * disqus_comments_dark.html
         *
         *
         */

        disqus.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                new Handler().postDelayed(disqus::reload, 2000); // Reload Comments
                super.onReceivedError(view, request, error);
            }
        });
        CookieManager.getInstance().setAcceptThirdPartyCookies(disqus, true); // Accept Cookies to login (If you forget this part users need to login every single time)
        disqus.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // Google / Facebook Login
        disqus.getSettings().setSupportMultipleWindows(true); // Google / Facebook Login
        CookieManager.getInstance().setAcceptCookie(true); // Accept Cookies to login 2
        disqus.setWebChromeClient(new WebChromeClient() {
            @SuppressLint("SetJavaScriptEnabled")
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                WebView newWebView = new WebView(context); // Create new WebView
                WebSettings webSettings = newWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webSettings.setUserAgentString(webSettings.getUserAgentString().replace("; wv", "")); // Hide WebView User Agent
                final Dialog dialog = new Dialog(context); // Create Dialog
                dialog.setContentView(newWebView);
                dialog.show();
                CookieManager.getInstance().acceptThirdPartyCookies(newWebView);
                newWebView.setWebViewClient(new WebViewClient());

                newWebView.setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onCloseWindow(WebView window) {
                        dialog.dismiss(); // Close the dialog after logged in
                    }
                });

                ((WebView.WebViewTransport) resultMsg.obj).setWebView(newWebView);
                resultMsg.sendToTarget();

                return true;
            }
        });
        disqus.getSettings().setJavaScriptEnabled(true); // Enable JavaScript
        disqus.getSettings().setAppCacheEnabled(true);
        disqus.getSettings().setDomStorageEnabled(true);
        disqus.loadUrl(commentsUri);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

First create a new WebView or find it with findViewById(R.id.webView); Then setup Disqus to your WebView: setupDisqus(this,webView);

More information : https://help.disqus.com/en/articles/1717165-javascript-embed-in-native-apps

Upvotes: 0

bichotll
bichotll

Reputation: 469

Thanks ndgreen. Seeing your idea I created a something different without a PHP file necesity: https://gist.github.com/bichotll/5563926

This script just create the html from a simple function and load th.

Upvotes: 5

ndgreen
ndgreen

Reputation: 167

I ran into the same problem with having my web site disqus threads linked up with my android app. I wrote a little walkthrough if you are interested I will link to my walkthrough below. Basically in your android app you want to use a WebView and use a separate php file that can take your disqus identifier.

http://globeotter.com/blog/disqus-android-code/

Upvotes: 10

Related Questions