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
- Kotlin
- Java
// 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)
// String message
PortalsPlugin.publish("cart", items);
// JSON message
JSONArray items = new JSONArray();
items.put("cheese");
items.put("bacon");
items.put("eggs");
PortalsPlugin.publish("cart", items);
Parameters
Name | Type | Description |
---|---|---|
topic | String | The topic associated with the message. Subscribers of this topic will receive the message |
data | Any | A 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
- Kotlin
- Java
// 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()
}
}
// Subscribe to the "dismiss" topic and check for
// a specific string to act on
PortalsPlugin.subscribe("dismiss", (result -> {
boolean doDismiss = result.getData().equals("cancel")
|| result.getData().equals("success");
if(doDismiss) {
this.dismiss();
}
return Unit.INSTANCE;
}));
Parameters
Name | Type | Description |
---|---|---|
topic | String | The topic to subscribe to |
data | SubscriptionResult -> Unit | A function to receive and handle the message |
unsubscribe
Unsubscribe from messages sent to a certain topic from the web application.
Usage
- Kotlin
- Java
// 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)
// 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
int subscription = PortalsPlugin.subscribe("dismiss", (result -> {
boolean doDismiss = result.getData().equals("cancel")
|| result.getData().equals("success");
if(doDismiss) {
this.dismiss();
}
return Unit.INSTANCE;
}));
// Unsubscribe from "dismiss"
PortalsPlugin.unsubscribe("dismiss", subscription);
Parameters
Name | Type | Description |
---|---|---|
topic | String | The topic to unsubscribe from |
subscriptionRef | Int | A reference to the subscription |