Web view communication

To set up WebView communications, you need programming skills, especially in JavaScript, to handle interactions between the app and web content.

What is WebView communication? WebView Communications allow a web view embedded in a mobile to interact with the app by exchanging variables and triggering actions, enhancing the app's functionality and user experience. This involves using HTML, CSS, and JavaScript to create dynamic content that communicates with the app through an API.

How Does WebView Work in Bravo?

The crucial feature of WebView is its ability to communicate with the app that contains it. This is done by passing variables and sending actions between the WebView and the app.

  • Passing Variables: Variables can be sent to the WebView from the app. The variable should be called in the HTML. For instance, if you need to show an interactive map with specific data, you can pass that data from the app to the WebView to dynamically customize the content.

  • Triggering Actions from WebView: The WebView can also trigger actions in the app. Here are the actions that can be used: "goto", "goback", "openurl", "refresh", "logout". For example, in an autocomplete search function: once the user selects an item, the WebView can send an action to Bravo to navigate to a detail page, seamlessly transitioning from a web view to a native view.

Technical Implementation

HTML and JavaScript

The content of the WebView is essentially HTML, CSS, and JavaScript. This content is developed outside of Bravo but can interact with Bravo through an API provided by the app.

Messaging Between WebView and the App

For the WebView and the app to communicate, a technique similar to iframes in web development is used. Communication is done through messages sent and received between the app and the WebView.

๐Ÿƒ Use Case: Swipe Functionality with WebView Communication

This is a generic WebView component with swipe functionality, similar to what you might find in a dating app. As the user swipes left or right, it triggers a go-to action within Bravo to display a pop-up of the swiped item.

To achieve this, you can use WebView Communication within Bravo. Here's how:

  1. Create an HTML which contains the following:

    1. Data Loading: It fetches data from a specified URL to populate the cards with information.

    2. Interaction Setup: A swipe-based interaction system for cards, allowing users to swipe left or right to navigate through content.

    3. Bravo Integration: Upon completing a swipe, the code sends a message to Bravo via WebView Communication, indicating the direction of the swipe and triggering corresponding actions within the Bravo app. For instance, when a user swipes left or right, trigger a go-to action within Bravo to display a pop-up of the swiped item.

Below is a demonstration of the code interacting with the WebView:

if (successful) {
    // throw card in the chosen direction
    this.topCard.style.transform =
        'translateX(' + posX + 'px) translateY(' + posY + 'px) rotate(' + deg + 'deg)'

    // send message to bravo
    if (direction_page) {
        const params_str = btoa(`{"name":"${this.topCard.textContent}"}`)
        const href_remote_str = `https://apps-service.bravostudio.app/devices/apps/${appid}/node/${direction_page}?params=${params_str}`
        const message = { "action": "goto", "params": {"href": `${direction_page}`,"hrefRemote": href_remote_str} };
        console.log(message)
        if (window.bravo != undefined) { window.bravo.postMessage(message); }
    }
    // wait transition end
    setTimeout(() => {
        // remove swiped card
        this.board.removeChild(this.topCard)

        // add new card
        this.push()
        // handle gestures on new top card
        this.handle()
    }, 200)
}
  • If the swipe gesture is successful (if (successful)), a message is constructed containing information about the swipe direction and the content of the swiped card.

  • The constructed message is then sent to Bravo using window.bravo.postMessage(message).

Message Construction: message is constructed as a JavaScript object containing two main properties:

  • "action": Specifies the type of action to be performed by the Bravo app. In this case, it is set to "goto", indicating a navigation action.

  • "params": Contains parameters related to the action, including:

    • "href": Specifies the destination page within the Bravo app. It is set to direction_page, which determines the specific page or screen to navigate to.

    • "hrefRemote": Specifies the remote URL constructed earlier (href_remote_str), which provides additional information or parameters for the Bravo app to process during navigation.

In this example, this is where the interaction with WebView Communication occurs, as it sends data from the web content (the swiped card) to the Bravo app for further processing or action triggering.

Using WebView in Bravo opens up many possibilities for creating more dynamic and functional hybrid apps. It allows combining the best of web and native worlds, offering an enriched user experience and advanced functionalities that wouldn't be possible otherwise.

For more in-depth information and to understand the basics of window communication in JavaScript, you can check out this detailed article.

Example file

Duplicate the files below to see how to set it up!

Your feedback is valuable to us. If you have any suggestions, questions, or issues, please don't hesitate to reach out to the community.

Last updated