Docs / Appmost JavaScript bindings

Appmost JavaScript Bindings

Documentation for Appmost's JavaScript bindings, available in Appmost Hybrid+ apps.

Functions

openBankId

getFirebasePushToken

getPushToken

clearWebViewCache

setInitialUrlOverride

showDialog

addUrlToOpenInternally

clearWebViewCacheWithReload

clearUrlListToOpenInternally

showFreshChat

downloadPdf

iOS vs Android

Calling native bindings

Native JavaScript bindings are invoked differently on iOS and Android. Use a helper that checks the user agent and calls the appropriate function. The example below also demonstrates callbacks and retrieving push tokens with or without Firebase.

 <script type = "text/javascript">

        /// Reusable function to call iOS and Android native bindings in an Appmost app
        /// Example: callNativeFunction("showDialog", { title: "Title here", message: "Message here"});
        function callNativeFunction(functionName, parameters) {
            let functionObj = { function: functionName }
            let methodCall = { ...functionObj, ...parameters };
            const iOSUserAgents = ['ios', 'iphone', 'ipad'];
            const userAgentIncludesIOS = iOSUserAgents.some((t) => navigator.userAgent.includes(t));
            if (userAgentIncludesIOS) {
                if (window.webkit?.messageHandlers?.appmostCallback) {
                    window.webkit.messageHandlers.appmostCallback.postMessage(methodCall);
                }
            } else if (navigator.userAgent.includes("android")) {
                let json = JSON.stringify(methodCall);
                appmostCallback.postMessage(json);
            }
        }

        /// Only to be used when Firebase is used for Push Notifications
        function getFirebaseToken() {
            callNativeFunction("getFirebasePushToken", { callbackFunction: 'didReceiveFirebasePushToken' })
        }

        function getPushToken() {
            callNativeFunction("getPushToken", { callbackFunction: 'didReceivePushToken' })
        }

        function didReceiveFirebasePushToken(token, error) {
            if (token != null) {
                alert ("Got token: " + token);
            } else {
                alert ("Got token error: " + error);
            }
        }

        function didReceivePushToken(token, error) {
            if (token != null) {
                alert ("Got token: " + token);
            } else {
                alert ("Got token error: " + error);
            }
        }
        
    </script>

More examples

 <script type = "text/javascript">

        function clearWebViewCacheWithReload() {
            callNativeFunction('clearWebViewCacheWithReload', {})
        }

        function showDialog() {
            callNativeFunction("showDialog", { title: "Titel", message: "Testar"} )
        }

        function includeUrl() {
            // Leading "https", "http", and "www" are ignored when comparing URLs in Appmost
            callNativeFunction("addUrlToOpenInternally", { url: "www.domain.here/optional_path", showDialog: "true"})
        }
        
 </script>