[Huawei Developers] Freemind R:
The original article is from HUAWEI Developer Forum

Forum link: https://forums.developer.huawei.com/forumPortal/en/home

​

Applications built specifically for mobile have been with us for more than 10 Years. Within that period the mobile development market has changed a lot. The way developers are approaching towards creating apps are not limited, in fact they have created their own ways for shortening development time. And that’s how cross-platform development was born.

Cross-platform, such as Cordova, Ionic, React Native, Flutter etc., are gaining a lot of attention especially for web developers who has knowledge about HTML,CSS, JavaScript etc. These platforms are also offering better opportunities to build native like experiences for developers.

Today we are going to learn, how to create Apache Cordova Plugin from scratch using HMS Core Capabilities.

Prerequisite

We must have latest version of Node installed on our machine.

We must have latest version of Visual Studio Code installed on our machine.

Once we installed Node, we can run the following command using command prompt (cmd) from anywhere in our environment to install Plugman and Cordova globally, so that it is available from any directory:

$ npm install -g plugman

$ npm install -g cordova

-g here indicates that Plugman installation is globally supported.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200427171159.31962684284690119465444611626650:50510522091346:2800:3EA6BBBEB9FCC638D32ACAEF5340EEF7670034055CDCF4DCF60B827C6249BC8E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200427171159.31962684284690119465444611626650:50510522091346:2800:3EA6BBBEB9FCC638D32ACAEF5340EEF7670034055CDCF4DCF60B827C6249BC8E.png[/IMG]

Why Plugman?

Plugman is a useful command line tool for installing and uninstalling or managing Cordova plugins. It supports multiple platform such as Android, iOS etc.

Let’s do it

First we create a separate folder in our system, where we want to create plugin. After we find a separate space, we copy the location and open command prompt to go to the specific location using cd command:

$ cd your/specific/location

Creating Plugin

We create plugin using below command.

$ plugman create --name HMSPushPlugin --plugin_id com.huawei.hmspushplugin --plugin_version 1.0.0

In the above command we are providing

a) Name of the plugin

b) Plugin id

c) Plugin version.

After creation of plugin, we use cd command to go to plugin location.

$ cd HMSPushPlugin

Adding Platform

We will add Android platform using below command.

$ plugman platform add --platform_name android

Create package.json

We will create package.json file using below command.

$ plugman createpackagejson .

Answer the questions presented, and you will end up with a package.json that looks like this:

{
"name": "hmspushplugin",
"version": "1.0.0",
"description": "hms push kit",
"cordova": {
"id": "com.huawei.hmspushplugin",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android"
],
"author": "sanghati",
"license": "ISC"
}

Folder Structure

After going through all the above command, the structure of the plugin creation will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200427172137.74719333123075251729506424903341:50510522091346:2800:9C73CD547A86DCCEC2E1199A521EE14354BF3519D43DB9F9A7D8F7278B296295.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200427172137.74719333123075251729506424903341:50510522091346:2800:9C73CD547A86DCCEC2E1199A521EE14354BF3519D43DB9F9A7D8F7278B296295.png[/IMG]

Plugin.xml is one of the most important files here. It’s what Cordova parses to figure out what platforms your plugin supports, preferences it has that the user can configure, source files we’ll include in our final app build, and more. Now if you peek into src folder, you may end up seeing native codes of Android or iOS or both. Finally, www is where the JavaScript code for our plugin lives. This is what gets called first and then kicks off Cordova to call our native code.

Role Play of Visual Studio Code

Now we open VS code and open the folder of the plugin and start modifying the plugin.xml file it is where everything happens and it’s where we should start first.

Modify the plugin.xml as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.huawei.hmspushplugin"
version="1.0.0">
<name>HMSPushPlugin</name>

<!-- Js file -->
<js-module
name="HMSPushPlugin"
src="www/HMSPushPlugin.js">
<clobbers target="cordova.plugins.HMSPushPlugin" />
</js-module>

<!-- Platform is added here -->
<platform name="android">
<framework src="src/android/build.gradle" custom="true" type="gradleReference" />
<!-- Push kit dependency -->
<framework src="com.huawei.hms:push:4.0.0.300"/>

<config-file parent="/*" target="res/xml/config.xml">
<feature name="HMSPushPlugin">
<param
name="android-package"
value="com.huawei.hmspushplugin.HMSPushPlugin.HMSPushPlugin" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />

<!-- Android source file is added here -->
<source-file src="src/android/HMSPushPlugin.java" target-dir="src/com/huawei/hmspushplugin/HMSPushPlugin" />
<source-file src="src/android/MessageService.java" target-dir="src/com/huawei/hmspushplugin/HMSPushPlugin"/>
</platform>
</plugin>

If we want to change the id or name then we need to give our plugin a unique name and id. As far as naming conventions go, the id is usually of the form this-is-my-plugin, like for example huawei-cordova-pushkit-plugin.

The js-module specifies the JavaScript code that will run, and <clobbers> sets what variable the plugin will be exported under. So, in this case, our script file is www/ HMSPushPlugin.js, and when our app runs, we’ll be able to access the plugin using cordova.plugins.HMSPushPlugin.

We specify the platforms we are going to support, along with references to the corresponding native code for each. Here we added Android as platform. Inside of <config-file> we specify our Android package name and also the symbol cordova will use to identify our plugin, in this case it’s HMSPushPlugin. Finally, we have a reference to our main Java code inside of <source-file> which is where our native code lives.

Adding HMS Push Kit

As we have mentioned two <source-file> in plugin.xml file. So, we need to create add two java file in our android directory.

This is not all. For more information about this, you can visit HUAWEI Developer Forum.