Skip to main content

PortalsPlugin

The PortalsPlugin class is a special Capacitor Plugin within the Portals library that allows for bi-directional communication between Android code and Web code. It is loaded with every Portal automatically and does not need to be added like other plugins.

Types

SubscriptionResult

An Object defining data received from the web application to a Subscriber

class SubscriptionResult(
val topic: String,
val data: Any,
val subscriptionRef: Int

fun toJSObject(): JSObject
}

Methods

publish

Send a message to the web application.

Usage

// String message
PortalsPlugin.publish("topic", "message content")

// JSON message
val items = JSONArray()
items.put("cheese")
items.put("bacon")
items.put("eggs")

PortalsPlugin.publish("cart", items)

Parameters

NameTypeDescription
topicStringThe topic associated with the message. Subscribers of this topic will receive the message
dataAnyA message to send. Note: this is added into a JSONObject to send through the Capacitor Bridge and should be a compatible type: JSONObject, JSONArray, String, Boolean, Integer, Long, Double, or null

subscribe

Subscribe to receive messages from the web application.

Usage

// Subscribe to the "dismiss" topic and check for
// a specific string to act on
PortalsPlugin.subscribe("dismiss") { result ->
if (result.data == "cancel" || result.data == "success") {
this.dismiss()
}
}

Parameters

NameTypeDescription
topicStringThe topic to subscribe to
dataSubscriptionResult -> UnitA function to receive and handle the message

unsubscribe

Unsubscribe from messages sent to a certain topic from the web application.

Usage

// Subscribe to the "dismiss" topic and check for
// a specific string to act on. Store the result of subscribe
// to keep a reference to unsubscribe with later
val subscription = PortalsPlugin.subscribe("dismiss") { result ->
if (result.data == "cancel" || result.data == "success") {
this.dismiss()
}
}

// Unsubscribe from "dismiss"
PortalsPlugin.unsubscribe("dismiss", subscription)

Parameters

NameTypeDescription
topicStringThe topic to unsubscribe from
subscriptionRefIntA reference to the subscription