WebMessageCompat

public class WebMessageCompat


The Java representation of the HTML5 PostMessage event. See https://html.spec.whatwg.org/multipage/comms.html#the-messageevent-interfaces for definition of a MessageEvent in HTML5.

Summary

Constants

static final int

Indicates the payload of WebMessageCompat is JavaScript ArrayBuffer.

static final int

Indicates the payload of WebMessageCompat is String.

Public constructors

@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
WebMessageCompat(@NonNull byte[] arrayBuffer)

Creates a WebMessage with JavaScript ArrayBuffer payload.

Creates a WebMessage with String payload.

@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
WebMessageCompat(
    @NonNull byte[] arrayBuffer,
    @Nullable WebMessagePortCompat[] ports
)

Creates a WebMessage with JavaScript ArrayBuffer payload.

WebMessageCompat(
    @Nullable String data,
    @Nullable WebMessagePortCompat[] ports
)

Creates a WebMessage with String payload.

Public methods

@NonNull byte[]

Returns the ArrayBuffer data of message.

@Nullable String

Returns the String data of the message.

@Nullable WebMessagePortCompat[]

Returns the ports that are sent with the message, or null if no port is sent.

int

Returns the payload type of the message.

Constants

TYPE_ARRAY_BUFFER

Added in 1.8.0
public static final int TYPE_ARRAY_BUFFER = 1

Indicates the payload of WebMessageCompat is JavaScript ArrayBuffer.

TYPE_STRING

Added in 1.8.0
public static final int TYPE_STRING = 0

Indicates the payload of WebMessageCompat is String.

Public constructors

WebMessageCompat

Added in 1.8.0
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public WebMessageCompat(@NonNull byte[] arrayBuffer)

Creates a WebMessage with JavaScript ArrayBuffer payload.

Parameters
@NonNull byte[] arrayBuffer

the array buffer data of the message.

WebMessageCompat

Added in 1.1.0
public WebMessageCompat(@Nullable String data)

Creates a WebMessage with String payload.

Parameters
@Nullable String data

the string of the message.

WebMessageCompat

Added in 1.8.0
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public WebMessageCompat(
    @NonNull byte[] arrayBuffer,
    @Nullable WebMessagePortCompat[] ports
)

Creates a WebMessage with JavaScript ArrayBuffer payload.

Parameters
@NonNull byte[] arrayBuffer

the array buffer data of the message.

@Nullable WebMessagePortCompat[] ports

the ports that are sent with the message.

WebMessageCompat

Added in 1.1.0
public WebMessageCompat(
    @Nullable String data,
    @Nullable WebMessagePortCompat[] ports
)

Creates a WebMessage with String payload.

Parameters
@Nullable String data

the string data of the message.

@Nullable WebMessagePortCompat[] ports

the ports that are sent with the message.

Public methods

getArrayBuffer

Added in 1.8.0
public @NonNull byte[] getArrayBuffer()

Returns the ArrayBuffer data of message. A ArrayBuffer or Transferable ArrayBuffer can be received from JavaScript. This should only be called when getType returns TYPE_ARRAY_BUFFER. Example:

WebMessageCompat message = ... // The WebMessageCompat received or prepared.
if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
    byte[] arrayBuffer = message.getArrayBuffer();
    // Access arrayBuffer data here.
}
Returns
@NonNull byte[]

ArrayBuffer payload data.

getData

Added in 1.1.0
public @Nullable String getData()

Returns the String data of the message. This should only be called when getType returns TYPE_STRING. Example:

WebMessageCompat message = ... // The WebMessageCompat received or prepared.
if (message.getType() == WebMessageCompat.TYPE_STRING) {
    String string = message.getData();
    // Access string data here.
}
Returns
@Nullable String

String payload data.

getPorts

Added in 1.1.0
public @Nullable WebMessagePortCompat[] getPorts()

Returns the ports that are sent with the message, or null if no port is sent.

getType

Added in 1.8.0
public int getType()

Returns the payload type of the message.

Returns
int

the payload type of WebMessageCompat.