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:
constconfig = newAndroidMessagingConfig({ // 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.
Native integration
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)
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:
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:
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.Native integration
Assuming you have the following:
@JavascriptInterface
named"ContentScopeScripts"
"featureA"
"featureA"
called"helloWorld"
Then delivering a NotificationMessage to it, would be roughly this in JavaScript (remember
params
is optional though)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"
Responding to a RequestMessage, or pushing a SubscriptionEvent
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:$messageCallback
matches AndroidMessagingConfig.messageCallback$messageSecret
matches AndroidMessagingConfig.messageSecret$message
is JSON string that represents one of MessageResponse or SubscriptionEvent