Android shared messaging configuration. This class should be constructed once and then shared between features (because of the way it modifies globals).

For example, if Android is injecting a JavaScript module like C-S-S which contains multiple 'sub-features', then this class would be instantiated once and then shared between all sub-features.

The following example shows all the fields that are required to be passed in:

const config = new AndroidMessagingConfig({
// a value that native has injected into the script
messageSecret: 'abc',

// the name of the window method that android will deliver responses through
messageCallback: 'callback_123',

// the `@JavascriptInterface` name from native that will be used to receive messages
javascriptInterface: "ARandomValue",

// the global object where methods will be registered
target: globalThis
});

Once an instance of AndroidMessagingConfig is created, you can then use it to construct many instances of Messaging (one per feature). See examples/android.example.js for an example.

Assuming you have the following:

  • a @JavascriptInterface named "ContentScopeScripts"
  • a sub-feature called "featureA"
  • and a method on "featureA" called "helloWorld"

Then delivering a NotificationMessage to it, would be roughly this in JavaScript (remember params is optional though)

const secret = "abc";
const json = JSON.stringify({
context: "ContentScopeScripts",
featureName: "featureA",
method: "helloWorld",
params: { "foo": "bar" }
});
window.ContentScopeScripts.process(json, secret)

When you receive the JSON payload (note that it will be a string), you'll need to deserialize/verify it according to "Messaging Implementation Guide"

If you receive a RequestMessage, you'll need to deliver a MessageResponse. Similarly, if you want to push new data, you need to deliver a SubscriptionEvent. In both cases you'll do this through a global window method. Given the snippet below, this is how it would relate to the AndroidMessagingConfig:

object ReplyHandler {
fun constructReply(message: String, messageCallback: String, messageSecret: String): String {
return """
(function() {
window['$messageCallback']('$messageSecret', $message);
})();
""".trimIndent()
}
}

Constructors

  • Parameters

    • params: {
          debug: boolean;
          javascriptInterface: string;
          messageCallback: string;
          messageSecret: string;
          target: Record<string, any>;
      }
      • debug: boolean
      • javascriptInterface: string

        the name of the javascript interface registered on the native side

      • messageCallback: string

        the name of the callback that the native side will use to send messages back to the javascript side

      • messageSecret: string

        a secret to ensure that messages are only processed by the correct handler

      • target: Record<string, any>

    Returns AndroidMessagingConfig

Properties

_capturedHandler: (json: string, secret: string) => void
debug: boolean
javascriptInterface: string
messageCallback: string
messageSecret: string
target: Record<string, any>

Methods

  • Assign the incoming handler method to the global object. This is the method that Android will call to deliver messages.

    Returns void

  • Capture the global handler and remove it from the global object.

    Returns void

  • Parameters

    • fn: (...args: any[]) => any
    • Optionalcontext: string = 'none'

    Returns any