Skip to main content

PortalMethod

The @PortalMethod annotation is used to easily create functions to use with the PortalsPlugin. A function with a @PortalMethod can be triggered by using the Portals.sendMessage() function from the PortalsPlugin class with the message parameter on the web code matching the function name in the native code.

The below example shows how to setup a function nativeFunction on an Android Fragment and call it from the web code.

Android

class MyPortalFragment : PortalFragment() {
// ...
public fun onCreate(savedInstanceState: Bundle?): Unit {
super.onCreate(savedInstanceState)
// ...
this.linkMessageReceivers(this)
}

@PortalMethod
public fun nativeFunction(payload: String): Unit {
// run native code here
}
}

Web

import { Portals } from '@native-portal/portals'

Portals.sendMessage({ message: 'nativeFunction', payload: result })