Subscribing to web messages
Web apps presented through a Portal can publish messages to native mobile code using the publish/subscribe interface (pub/sub) available with Portals.
In this step, you will subscribe to messages sent from the web to dismiss the current view.
Exploring the problem
Web apps consolidated into the Jobsync superapp may consist of multiple views, and therefore need to manage navigation within their scope. Each web app designed for this training contains a header with a back button and when the web app has exhausted its navigation stack, it uses the Portals web library to communicate with Android code by sending a message to the navigate:back
topic.
In the section below, you will use the Portals Android library to subscribe to the navigate:back
topic and dismiss the view when a message is received.
Creating a subscriber
The pub/sub mechanism included in the Portals Android library relies on two parts that work together: PortalsPubSub
and PortalsPlugin
. PortalsPlugin
is a Capacitor plugin (you will learn about those in the next step) added to a Portal by default that allows web apps to communicate with the native layer and vice-versa. PortalsPubSub
is the class that manages an internal message bus.
Pub/sub is bi-directional; messages can be sent from native mobile code to web code as well.
Modify portals/WebAppScreen.kt
to a subscribe for the navigate:back
topic:
Add an instance of PortalsPlugin
to the Portal.
Create an instance of the PortalsPubSub
class and pass it into the PortalsPlugin
constructor.
Subscribe to the navigate:back
topic, and pop the nav stack when the topic receives a message.
Clean up the subscription once it's no longer needed by unsubscribing to the navigate:back
topic.
Add an instance of PortalsPlugin
to the Portal.
Create an instance of the PortalsPubSub
class and pass it into the PortalsPlugin
constructor.
Subscribe to the navigate:back
topic, and pop the nav stack when the topic receives a message.
Clean up the subscription once it's no longer needed by unsubscribing to the navigate:back
topic.
Additional ways to create subscribers can be found at this link.
Testing the subscription
Build and run the Jobsync app and navigate to one of the features in the dashboard view. Switch from the 'Initial Context' tab to the 'Publish/Subscribe' tab.
Here, you can test Portal's pub/sub mechanism by entering navigate:back
into the 'Topic:' input field under the 'Publish' heading. Press the 'Publish' button, and the view should dismiss, navigating back to the dashboard view.
What's next
The pub/sub mechanism available within the Portals library is ideal for simple use cases, such as allowing a web app presented through a Portal to request native navigation. However, it is not suitable for more complex use cases. In the next step of this module, you will learn about Capacitor plugins, which also communicate bi-directionally, but in a more structured manner suitable for complex use cases.