Search code examples
androidauthenticationwebviewpushpersonalization

Android WebView Push assign name to DeviceID


I am developing an Android App with WebView and Push notifications. The problem is, that the website i'm displaying has a login.

What I want to do is: If somebody logs into the website via my App (WebView!), I need to take the username he entered and put it into my MySQL-Database with the Android-Device-ID. (but just, if his password was correct..)

Does anybody know of an elegant way to do this? I'm stuck.


Solution

  • I found a solution. I put an alertdialog over the websites original Login. After this, I injected Java Script Code into the URL, which fills out the forms on the website and then automatically submits. If anybody is interested, I did it like this:

    AlertDialog for the logIn:

        private void logIn() {
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.activity_login, null);
            final EditText username = (EditText) textEntryView.findViewById(R.id.username);
            final EditText password = (EditText) textEntryView.findViewById(R.id.password);
            username.setText("", TextView.BufferType.EDITABLE);
            password.setText("", TextView.BufferType.EDITABLE);
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
            alert.setTitle(Html.fromHtml("<font color='#ffffff'>Login</font>"));
            alert.setView(textEntryView);
            alert.setPositiveButton("Login",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            User = username.getText().toString();
                            Pw = password.getText().toString();
                            autoFillIn();
                        }
                    });
            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                        }
                    });
            alert.show();
    }
    

    And then autoFillIn:

        public void autoFillIn(){
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                view.loadUrl("javascript:document.getElementById('LoginForm_username').value = '" + User + "';document.getElementById('LoginForm_password').value='" + Pw + "';document.forms[0].submit();");
            }
        });
        mWebview .loadUrl(frontendurl);
        setContentView(mWebview);
    
    }
    

    Hope this might help somebody. And i'm always open for better ideas..

    Edit: "frontendurl" is a string with the url i want to display via webview..