Communicating Between JavaScript and Java Through the Cordova Plugins in HMS Core Kits

Jackson
Huawei Developers
Published in
5 min readNov 26, 2021

Background

Cordova is an open-source cross-platform development framework that allows you to use HTML and JavaScript to develop apps across multiple platforms, such as Android and iOS. So how exactly does Cordova enable apps to run on different platforms and implement the functions? The abundant plugins in Cordova are the main reason, and free you to focus solely on app functions, without having to interact with the APIs at the OS level.

Introduction

Here, I’ll use the Cordova plugin in HUAWEI Push Kit as an example to demonstrate how to call Java APIs in JavaScript through JavaScript-Java messaging. The following implementation principles can be applied to all other kits, except for Map Kit and Ads Kit (which will be detailed later), and help you master troubleshooting solutions.

Basic Structure of Cordova

When you call style=’mso-bidi-font-weight:normal’>loadUrl in MainActivity, CordovaWebView will be initialized and Cordova starts up. In this case, style=’mso-bidi-font-weight:normal’>CordovaWebView will create style=’mso-bidi-font-weight:normal’>PluginManager, NativeToJsMessageQueue, as well as ExposedJsApi of JavascriptInterface. style=’mso-bidi-font-weight:normal’>ExposedJsApi and NativeToJsMessageQueue will play a role in the subsequent communication.

During the plugin loading, all plugins in the configuration file will be read when the PluginManager object is created, and plugin mappings will be created. When the plugin is called for the first time, instantiation is conducted and related functions are executed.

A message can be returned from Java to JavaScript in synchronous or asynchronous mode. In Cordova, set async in the method to distinguish the two modes.

In synchronous mode, Cordova obtains data from the header of the NativeToJsMessageQueue queue, finds the message request based on callbackID, and returns the data to the success method of the request.

In asynchronous mode, Cordova calls the loop method to continuously obtain data from the NativeToJsMessageQueue queue, finds the message request, and returns the data to the success method of the request.

In the Cordova plugin of Push Kit, the synchronization mode is used.

Plugin Call

You may still be unclear on how the process works, based on the description above, so I’ve provided the following procedure:

  1. Install the plugin.

The plugin.xml file records all information to be used, such as JavaScript and Android classes. During the plugin initialization, the classes will be loaded to Cordova. If a method or API is not configured in the file, it is unable to be used.

2. Create a message mapping.

The plugin provides the methods for creating mappings for the following messages:

(1) HmsMessaging

In the HmsPush.js file, call the runHmsMessaging API in asynchronous mode to transfer the message to the Android platform. The Android platform returns the result through Promise.

The message will be transferred to the HmsPushMessaging class. The execute method in HmsPushMessaging can transfer the message to a method for processing based on the action type in the message.

The processing method returns the result to JavaScript. The result will be written to the nativeToJsMessageQueue queue.

2) HmsInstanceId
In the HmsPush.js file, call the runHmsInstance API in asynchronous mode to transfer the message to the Android platform. The Android platform returns the result through Promise.
The message will be transferred to the HmsPushInstanceId class. The execute method in HmsPushInstanceId can transfer the message to a method for processing based on the action type in the message.

Similarly, the processing method returns the result to JavaScript. The result will be written to the nativeToJsMessageQueue queue.

This process is similar to that for HmsPushMessaging. The main difference is that HmsInstanceId is used for HmsPushInstanceId-related APIs, and HmsMessaging is used for HmsPushMessaging-related APIs.

3) localNotification
In the HmsLocalNotification.js file, call the run API in asynchronous mode to transfer the message to the Android platform. The Android platform returns the result through Promise.
The message will be transferred to the HmsLocalNotification class. The execute method in HmsLocalNotification can transfer the message to a method for processing based on the action type in the message.

Call sendPluginResult to return the result. However, for localNotification, the result will be returned after the notification is sent.

3. Perform message push event callback.
In addition to the method calling, message push involves listening for many events, for example, receiving common messages, data messages, and tokens.
The callback process starts from Android.
In Android, the callback method is defined in HmsPushMessageService.java.
Based on the SDK requirements, you can opt to redefine certain callback methods, such as onMessageReceived, onDeletedMessages, and onNewToken.

When an event is triggered, an event notification is sent to JavaScript.

Each event is defined and registered in HmsPushEvent.js.

Please note that the event initialization needs to be performed during app development. Otherwise, the event listening will fail. For more details, please refer to eventListeners.js in the demo.

If the callback has been triggered in Java, but is not received in JavaScript, check whether the event initialization is performed.

In doing so, when an event is triggered in Android, JavaScript will be able to receive and process the message. You can also refer to this process to add an event.

Summary

The description above illustrates how the plugin implements the JavaScript-Java communications. The methods of most kits can be called in a similar manner. However, Map Kit, Ads Kit, and other kits that need to display images or videos (such as maps and native ads) require a different method, which will be introduced in a later article.

References

--

--

Jackson
Huawei Developers

Software engineer | Android app development | Java | JS | Python | Machine learning