Basic steps to follow to integrate HMS capabilities in B4A - Huawei Developers

More articles like this, you can visit HUAWEI Developer Forum​
Downloading the AAR Packages and JSON File
o Sign in to HUAWEI Developer and download the AAR packages required.
o AAR packages related to HMS Push kit is displayed as below
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Refer below link for the steps.
https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0
Figure 1-2 Configuring app information in App Gallery Connect
Open AAR packages with rar tool and rename the class.jar and AndroidManifest.xml files. (It is recommended that the two files be renamed consistently with the AAR package names.)
Figure 1-3 Renaming files
Copy required permissions in the <manifest> section in B4A IDE
Figure 1-4 Permissions in the <manifest> section
Copy all configurations in the <application> section
Figure 1-5 All configurations in the <application> section
Change ${applicationId} to $PACKAGE$.
${applicationId} is the field for automatically obtaining the package name in Android Studio. Its counterpart on B4A is $PACKAGE$
Download the latest configuration file(agconnect-services.json) from App Gallery Connect
Figure 1-6 Downloading the JSON file
Adding the JSON File to the assets Folder
Figure 1-7 Adding the JSON file
B4A will automatically incorporate files in the assets folder of an AAR package to the assets folder of your main project.
Encapsulating Java Files Using SLC
Configure the Java project.
SLC wraps Java project and adds it to the B4A library. For demonstration purposes, we have created a Java project.
It is recommended that you name the first folder b4x in src. Otherwise, the library generated with SLC may not function properly.
Figure 1-8 Java project
Develop the Java project.
Name the Java files based on the src path.
Figure 1-9 Naming the files based on the src path
Import the following two lines of code to each Java file
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
Add necessary annotations to the Java files
Figure 1-10 Example annotations
Modify the context.
B4A does not recognize the Context class. Therefore, when parsing a class that contains the @version(1.0f) annotation, it will report an error if a method of the class has referenced Context. In this case, you need to change Context to the B4A object BA
Figure 1-11 Context example
The following describes the @Events annotation.
This annotation is used to report specific events in the Java method to the listener registered on B4A.
The code on B4A is also required to implement this function.
Take the getPushToken method in PushWork as an example. If you want to pass the push token in the Java method to a listener on B4A, do as follows:  
Set a listening event on B4A.
Figure 1-12 Setting a listening event
Add the @event annotation to the Java files. The code is shown in the following figure.
The value of NewToken in the red box must be the same as xxxx of Listener_xxxx in the listening event name added to B4A in the previous step.
Call the ba. raiseEventFromUI method in the method that needs to report the push token. The code is shown in the following figure.
Event reporting
Select the project folder, define the library name, and click Compile
Figure 1-14 Compilation successful
Select the Addition folder and add AAR packages to it.
Figure 1-15 Tools-Configure Paths
Go to Project > Build Configurations and change the B4A project package name to the project package name configured in App Gallery Connect
Figure 1-16 Changing the B4A project package name
Go to Tools > Private Sign Key and select the SHA256 signature file specified in AppGallery Connect as the B4A project signature file
Figure 1-17 Configuring the signature file
Declare MainApplication in the Manifest file.
The path of MainApplication must also be the path to the src folder where MainApplication is located.
Add the #AdditionalJar field to Project Attributes to reference the AAR packages
The value of #AdditionalJar is the name of the AAR package (with a file name extension) in the Addition folder

Related

How to download and Integrate the HUAWEI Account Kit SDK [Eclipse IDE]

1. Eclipse SDK Download
If you are using the Eclipse IDE, download the HMS SDK package and integrate it locally.
Integrating the HMS SDK into Your App Project in the Eclipse IDE
Before starting your development in the Eclipse Integrated Development Environment (IDE), download the HMS SDK and integrate it into your app project.
Downloading the SDK
1. Download and decompress the zip packages of the HMS Base SDK and HUAWEI Account SDK.
For details, please refer to Eclipse SDK Download.
2. Import all SDK projects to your workspace.
a. Go to File > Import > Android > Existing Android Code Into Workspace and click Next.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
b. Click Browse and navigate to the directory where your projects are located. Select the projects to be imported under Projects and click Finish. By default, all projects are selected. You can change as needed.
NOTE:The projects to be imported must be located in the same drive as the workspace, for example, D:\. If they are not in the same drive, select Copy projects into workspace to copy the project file to the workspace.
Adding Project Dependencies
1. Right-click your app project and choose Properties from the shortcut menu.
2. Choose Android from the navigation tree, click Add, select the imported projects, and click OK.
3. As the XML file in the Eclipse IDE does not support the dollar sign ($), replace ${applicationId} in the AndroidManifest.xml file of the HMSSdkBase and Agconnect-core projects with your app package name
Code:
<application>
<provider
android:name="com.huawei.agconnect.core.provider.AGConnectInitializeProvider"
android:authorities="${applicationId}.AGCInitializeProvider"
android:exported="false" />
<service
android:name="com.huawei.agconnect.core.ServiceDiscovery"
android:exported="false" />
</application>
Copying the HMSSdkBase Properties Files to Your App Project Directory
Copy the properties files in the assets directory of the HMSSdkBase project to the assets directory of your app project.
NOTE: If any other kits are involved, copy the properties files of these kits to your app project directory.
Configuring the project.properties File
Find and open the file in your project directory and add the following code to merge the manifest files in the subprojects:
Code:
manifestmerger.enabled=true
Adding the AppGallery Connect Configuration File
1. Sign in to AppGallery Connect and select My apps.
2. Find your app from the list and click the app name.
3. Go to Develop > Overview > App information.
4. Click agconnect-services.json to download the configuration file.
5. The Eclipse IDE does not support the AppGallery Connect plug-in. Therefore, you need to place the agconnect-services.json file in the assets directory of your project and add the following code to the Application subclass of the project. If the project does not have an Application subclass, you need to create a subclass that is inherited from the Application subclass. For example, create a MyApplication class.
Code:
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
}
// TODO: Add the following code:
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
AGConnectServicesConfig config = AGConnectServicesConfig.fromContext(context);
config.overlayWith(new LazyInputStream(context) {
public InputStream get(Context context) {
try {
return context.getAssets().open("agconnect-services.json");
} catch (IOException e) {
return null;
}
}
});
}
// TODO: End
}
You still need to import the following classes:
Code:
import java.io.IOException;
import java.io.InputStream;
import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.agconnect.config.LazyInputStream;
import android.app.Application;
import android.content.Context;
import android.util.Log;
6. Open the AndroidManifest.xml file of your project and add the Application subclass name to the application section. If the subclass name has been added, skip this step. In the following example, add the MyApplication class name.
Code:
<application
android:name=".MyApplication"
…
>
…
</application>
7. In the Eclipse IDE menu bar, click Project, then Clean.

Integrating Automatic Speech Recognition without Pickup UI in B4A Platform

Introduction
Automatic speech recognition (ASR) can recognize speech not longer than 60 Seconds and convert the input speech into text in real time. This service uses industry-leading deep learning technologies to achieve a recognition accuracy of over 95%. Currently, Mandarin Chinese (including Chinese-English bilingual speech), English, French, German, Spanish, and Italian can be recognized
Real-time result output
Available options: with and without speech pickup UI
Endpoint detection: Start and end points can be accurately located.
Silence detection: No voice packet is sent for silent part.
Intelligent conversion to digital formats: For example, the year 2020 can be recognized from voice input.
Follow all the steps mentioned in Basic Setup to start HMS integration on B4A Platform.
Refer to
https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201286424114350051&fid=0101246461018590361
Enable ML Kit in App gallery connect.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Creating Wrapper Class
1. Downloading the AAR Packages and JSON File
Sign in to HUAWEI Developer and download the AAR packages required.
2. Open AAR packages with rar tool and rename the class.jar and AndroidManifest.xml files. And save those jar file inside libs folder (It is recommended that the two files can be renamed consistently with the AAR package names.)
3. Copy required permissions in the <manifest> section in B4A IDE.
4. Copy all configurations in the <application> section.
5. Change ${applicationId} to $PACKAGE$.
6. Download the configuration file(agconnect-services.json) from App Gallery Connect
And Add the JSON File to the assets Folder of the AAR file as shown below.
B4A will automatically incorporate files in the assets folder of an AAR package to the assets folder of your main project.
Encapsulating Java Files Using SLC
1. Create Library as parent and then create bin, libs and src as subfolder in the project directory.
2. Develop java project inside the following path:
Choose Library Folder > src > b4x > asr > demo
3. Import the following two lines of code to each Java file.
Code:
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
import anywheresoftware.b4a.IOnActivityResult;
4. Add necessary annotations to the ASR Java files.
Code:
@Version(1.0f)@ShortName("Asr")@DependsOn(values={"agconnect-core-1.0.1.300.aar",
"tasks-1.3.1.302.aar",
"network-common-4.0.2.300.aar",
"network-grs-4.0.2.300.aar",
"okhttp-3.12.0.jar",
"okio-1.15.0.jar",
"ml-computer-agc-inner-2.0.1.300.aar",
"ml-computer-cloud-base-inner-2.0.1.300.aar",
"ml-computer-commonutils-inner-2.0.1.300.aar",
"ml-computer-ha-inner-2.0.1.300.aar",
"ml-computer-grs-inner-2.0.1.300.aar",
"ml-computer-net-2.0.1.300.aar",
"ml-computer-voice-asr-plugin-2.0.1.300.aar",
"ml-computer-vision-cloud-2.0.1.300.aar",
"ml-computer-voice-asr-2.0.1.300.aar"
})
@Permissions(values={"android.permission.INTERNET",
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.RECORD_AUDIO",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.CHANGE_WIFI_STATE",
"android.permission.ACCESS_WIFI_STATE",
"android.permission.CHANGE_CONFIGURATION",
"android.permission.WAKE_LOCK"})
More details, you can click https://forums.developer.huawei.com/forumPortal/en/topic/0203394430060880031
Very clear. Do you have a Repo with the code?

A Novice Journey of integrating Huawei Video Kit feat. Node js Local Server

Introduction
HMS Video Kit allow us to play video by using a URL or multiple URLs that contain an address of a video. The latest version of this kit allow us to playback videos but in the later version it will support both editing and hosting of videos.
This service helps us to build a superb video experience for our app users.
Use Cases
1) The service can be used in a news app that contains small videos of the reported incident.
2) The service can be used as a video editing app.
3) The service can be used as a promotion video in your application.
4) The service can be used as an educational video related application.
In this article we will be working on one of the use case and that is education video related application. For that we need two major players in any software development and that is server and client.
Demo
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Server Side (Node Server)
Prerequisite
1) We must have latest version of Node installed.
2) We must have latest version of Visual Studio Code installed.
3) Laptop/desktop and Huawei mobile device must share same Wi-Fi connection.
Tool’s required
1) Express js is a Node js web application server framework, designed for building single-page, multi-page, and hybrid web applications. It is the de facto standard server framework for node.js.
We don't have to repeat same code over and over again using Express js. Node.js is a low-level I/O mechanism which has an HTTP module. If you just use an HTTP module, a lot of work like parsing the payload, cookies, storing sessions, selecting the right route pattern based on regular expressions will have to be re-implemented. With Express.js, it is just there for us to use.
Express.js basically helps us manage everything, from routes, to handling requests and views.
2) Request module is by far the most popular (non-standard) Node package for making HTTP requests. Actually, it is really just a wrapper around Node's built in http module, so we can achieve all of the same functionality on your own with http, but request just makes it a whole lot easier.
Code:
const request = require('request');
request(' https://developer.huawei.com/consumer/en/', function(err, res, body) {
console.log(body);
});
The code above submits an HTTP GET request to developer.huawei.com and then prints the returned HTML to the screen. This type of request works for any HTTP endpoint, whether it returns HTML, JSON, an image, or just about anything else.The first argument to request can either be a URL string, or an object of options. Here are some of the more common options you'll encounter in our application:
a) url: The destination URL of the HTTP request
b) method: The HTTP method to be used (GET, POST, DELETE, etc.)
c) headers: An object of HTTP headers (key-value) to be set in the request
d) form: An object containing key-value form data
Creating Project
First we need to find a space in our machine and create a folder. We can name the folder whatever we want. Open Visual Studio Code, navigate to particular folder location which we created using cd command in the terminal of VS code. Run the below command to create package.json file.
Code:
npm init
Answer the questions presented, and you will end up with a package.json that looks like this:
Code:
{
"name": "android-node-server",
"version": "1.0.0",
"description": "It is a server for HMS Push Notification Kit",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sanghati",
"license": "ISC"
}
We need to run the above command when we start a new project. Also whatever tools we install in this project, a dependency will be added in the package.json file and we use the below command to install our dependencies in node_modules folder.
Code:
npm install
We may need to run this every time we manually add a dependency to our package.json file.
Install Express
We run the below command to install Express js in our project.
Code:
npm install express –save
--save, will ensure to add express dependency to our package.json file.
Create app.js file
To create app.js file using VS code terminal we need touch. Touch command creates an empty file if it doesn’t exist. Run the below command to create app.js file.
Code:
npm install -g touch-cli
touch app.js
More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0204400319008240095

How Can I Integrate Remote Configuration of AppGallery Connect in Flutter?

Recently, it is found that some services of AppGallery Connect start to support cross-platform frameworks, including Flutter. Let's have a try on Remote Configuration.
Integration Procedure
1. Install the Flutter environment.
a) Download the Flutter SDK package from https://flutter.dev/docs/get-started/install/windows.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Decompress the package to any directory, for example, D:\Flutter.
b) Add Flutter to environment variables. Here, the path is D:\Flutter\flutter_windows_1.22.2-stable\flutter\bin.
c) In Android Studio, go to File > Settings > Plugins, download the Flutter and Dart plug-ins, and restart Android Studio for the plug-ins to take effect.
2. Create a project and enable Remote Configuration.
a) Create an Android project in AppGallery Connect and enable Remote Configuration.
b) Add the following configuration items to Remote Configuration.
c) In Android Studio, go to File > New > New Flutter Project… and create a Flutter project.
d) Go to Project settings > General information in AppGallery Connect. In the App information area, download the agconnect-services.json file.
e) Place the agconnect-services.json file in the directory shown in the following figure.
f) Configure the Maven repository and Remote Configuration plug-in in the app-level build.gradle file.
g) Open the build.gradle file in the android/app directory of the Flutter project and add the build dependencies and AppGallery Connect plug-in address.
3. Integrate the Remote Configuration SDK.
Add the SDK dependency of Remote Configuration to the pubspec.yaml file.
Click Pub get to synchronize the data.
4. Test basic functions.
Here I implement a simple demo app to demonstrate several of the most important functions.
In the UI design, the Mode 1:Fetch And Activate Immediately, Mode 2:Fetch And Activate Next Time, and Clear Data buttons are provided to implement the following functions:
1. Obtain data from the cloud. This operation takes effect this time.
Tap Mode 1:Fetch And Activate Immediately to call fetch and then applyLastFetched to obtain the data fetched this time. The operation will take effect immediately. Therefore, we call the getMergedAll API to apply all the settings to the device. Then, we can obtain the data from the cloud on the device. The following figure shows the tap effect.
2. Obtain the cloud data and make the setting obtained last time take effect.
The API for delaying the setting to take effect shares the same implementation logic as that for making the setting take effect immediately, and they differ in the execution sequence.
Tap Mode 2:Fetch And Activate Next Time to call apply and getMergedAll to apply the last fetch result, and then call fetch to obtain the latest setting for next use.
3. Clear the data.
To clear data, tap Clear Data to call the clearAll API.
4. Set local configuration items.
You can create a Map parameter in the*.dart file and call the applyDefault API to apply the local settings.
HTML:
Map defaults = {
'mLong': 1000,
'mString': 'hello world',
'mDouble': 3.14,
'mBoolean': true
};
AGCRemotConfig.instance.applyDefaults(defaults);
5. Package your app.
Similar to Android, you only need to run your project in Android Studio.
Precautions
Different from setting local configuration items on the Android platform, Flutter cannot use XML files to store local configuration items.
For more details, check:
Flutter official guide: https://flutter.dev/docs/get-started/install/windows
To view more about how to integrate Remote Configuration into an Android project, visit https://developer.huawei.com/consum...-connect-Guides/agc-remoteconfig-introduction.
Demo of Remote Configuration(Flutter):https://developer.huawei.com/consum...oteconfig-flutter-samplecode-0000001056579535
Is there any usage restriction?
riteshchanchal said:
Is there any usage restriction?
Click to expand...
Click to collapse
there are some restriction as follows. For more restriction, please focus on the official document
A single project can have a maximum of 2000 parameters and 500 conditions. The total length of all parameter strings cannot exceed 800 KB.
The interface invoking frequency cannot exceed 20 times per hour.

Push Kit Integration with Cocos

Introduction:
This article will demonstrate to Integrate Push kit in Cocos using Huawei HMS Core App Services Plugin. Huawei Push Kit is a messaging service. It establishes a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on users devices in real time. This helps to maintain closer ties with users and increases user awareness and engagement with your apps.
Note: Cocos is free but not open source.
Requirements:
1. Cocos Creator tool
2. Notepad++ for editing and writing script
3. HMS Device
4. Android Studio for Analyzing APK and Checking Log
Output:
Push Notification, Data Message.
Steps to Integrate:
1. Install and launch cocos creator.
2. Make sure to configure NDK Root and Android SDK Root in Cocos Creator, choose File> Settings>Select Native Develop.
3. Click in Cocos Dashboard, click New.
Create project name in Name section and click on hello-world and then Create and open.
Click on Project tab and then Build.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Enter Platform, Build Path and Package Name in build window.
Note: The package name should be same in app gallery connect.
In the same build popup, in the keystore section we can use default keystore (default location is /Users/<username>/.android/debug.keystore) and we can generate SHA key or we can create custom keystore and can generate SHA key.In Command line terminal execute the command below and get SHA key:
Code:
keytool -list -v -keystore keystorepath\keystorename.keystore
Steps to create custom key store file:
a) Uncheck use debug keystore
b) In keystore path section click on New button.
c) Enter all the details in New Keystore details and then click Save.
4. Step 1: Create a project in AppGallery using created SHA key
Step 2: Configure the signing certificate fingerprint
Choose Project settings > General information. In the App information area, click the icon next to SHA 256 certificate fingerprint, and enter the obtained SHA-256 certificate fingerprint.
Step 3: Download agconnect-services.json file and put in setting folder in cocos project.
Example:
Right click on Assets folder and then select Open in Explorer
Place agconnect-services.json file inside settings folder
5. Click Enable for Cocos SDKHub in Service tab.
Click on Association button and register project in Cocos Console
Associating a Game in Cocos Creator You can enable third-party services for your app in Cocos Creator. But first, you need to set a Cocos AppID. Select a created game, and click Association, as shown in the following figures.
Select Plugin Manage under Service tab.
Select Push option in Plugin Manage under Service tab.
Place mouse cursor over the "Configured" text so that "Edit" and "Delete" icons will be appeared. Click on edit icon.
Put public key(from AppGallery connect) in the "Payment Public key" field and select "en" option for "Support language" field and then click OK.
Script code for Notification and Data message:
onLoad: function () {
sdkhub.getPushPlugin().startPush();
sdkhub.getPushPlugin().setListener(this.onPushResult, this);
},
onPushResult: function (code, msg) {
console.log("on push result action.");
console.log("code: " + code);
console.log("massage: " + msg);
this.label.string=msg;
this.showToast(code, msg);
switch (code) {
case sdkhub.PushResultCode.kStartPushSucceed:
console.log("kStartPushSucceed", msg);
break;
}
},
6. Click on Build, Compile, Play under the Project tab to generate APK file.
Tips & Tricks:
Make sure to keep the same package name as kept in the Huawei AppGallery Connect project.
Conclusion:
To promote products and launch marketing activities, games, e-commerce apps, and utility apps can call Push Kit APIs to select target audiences by tag and push messages to them, attracting more users and improving the marketing activity conversion rate.
References:
> Note: this version of the documentation has been archived and is no longer maintained. Please move to the latest version. · Cocos Creator
docs.cocos.com

Categories

Resources