S
S
supportitc2020-05-12 13:10:26
JavaScript
supportitc, 2020-05-12 13:10:26

Push notifications on WebView - how to implement in an Android application?

In my example, I connected the java functions of the application in Android Studio using the Javascript code on my site:

I connect the Javascript interface to the public MainActivity class:

myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");


For example, I set on the button (when pressed) - the event of calling a function on the site via JavaScript (Already in the code of the site itself):

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>


Next, I write the processing function when calling JS in the MainActivity of the application in Android Studio:

public class WebAppInterface {
        Context mContext;
        WebAppInterface(Context c) {
            mContext = c;
        }
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }


// As a result, when you click a button on the site: - we get a short TOAST message in the Android application.

The question is how TOAST can be replaced with a simple (of the same type) Push notification?

2f530e8e6d37d7e60f95ae3907b91f9d303b389f

Of course, Javascript itself will call the notification function on request from the Web server, but the only problem is that when the application is closed - naturally JS will not work, so you need to ensure that the application works in the background.
How would you implement it in the simplest way.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question