Beginner: Guide to integrate Huawei Ads Kit in Banking app (Kotlin) - Huawei Developers

Introduction
In this article, I will be integrating Huawei Ads Kit in an Application. I will be using Banner Ads. Banner Ads are rectangular ad images located at the top, middle or bottom of an application’s layout that are automatically refreshed at intervals and guides to the advertiser’s page when clicked.
Ads Kit
Huawei Ads Kit leverages the vast user base of Huawei devices and Huawei's extensive data capabilities to provide you with the Publisher Service, helping you to monetize traffic.
HMS Ads Kit has 7 types of Ads kits. Now we can implement Banner Ads in this application.
Use Case
The Ads Kit will be used to display Banner Ads in a Banking Application which can perform basic functions such as Enter Pin, Withdraw Money, Deposit Money and Check Balance.
Requirements
1. Any operating system (MacOS, Linux and Windows).
2. Must have a Huawei phone with HMS 4.0.2.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
4. Minimum API Level 21 is required.
5. Required EMUI 9.0.0 and later version devices.
Integrate HMS Dependencies
1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
2. Create a project in android studio, refer Creating an Android Studio Project.
3. Generate a SHA-256 certificate fingerprint.
4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in SHA256 CODE
5. Create an App in AppGallery Connect.
6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
7. Enter SHA-256 certificate fingerprint and click Save, as follows.
8. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
9. Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Ads Kit
Implementation 'com.huawei.hms:ads-lite:13.4.40.301'
10. Now Sync the gradle.
11. Add the required permission to the Manifestfile.xml file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--check wifi state-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Development
Adding Banner view to activity_main.xml
Code:
<com.huawei.hms.ads.banner.BannerView
android:id="@+id/banner_ADview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
hwads:adId="@string/banner_ADid"
hwads:bannerSize="BANNER_SIZE_360_57" />
Add this code to implement Banner Ads
Code:
//Initialize the Huawei Ads SDK
HwAds.init(this)
// To get Banner view from the activity_main.xml. It will display at bottom of the page.
val bottomBannerView = findViewById<BannerView>(R.id.banner_ADview)
val adParam = AdParam.Builder().build()
bottomBannerView.adId = getString(R.string.banner_ADid)
bottomBannerView.bannerAdSize = BannerAdSize.BANNER_SIZE_SMART
bottomBannerView.loadAd(adParam)
// Call new BannerView to create a BannerView class. It will display at top of the page.
val topBannerView = BannerView(this)
topBannerView.adId = getString(R.string.banner_ADid)
topBannerView.bannerAdSize = BannerAdSize.BANNER_SIZE_SMART
topBannerView.loadAd(adParam)
val rootView = findViewById<RelativeLayout>(R.id.root_view)
rootView.addView(topBannerView)
Cloud Debugging
Use Cloud Debugging in HMS Toolkit to debug the app on a real device.
To use Cloud Debugging, you need to sign in using a HUAWEI ID, complete identity verification, and then authorize the sign-in.
Choose HMS > CloudDebugging.
You can use Available Devices. Select a device and click RUN.
Result
{
"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"
}
Tips and Tricks
1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
2. Make sure you have added the agconnect-services.json file to app folder.
3. Make sure you have added SHA-256 fingerprint without fail.
4. Make sure all the dependencies are added properly.
Conclusion
In this article, we have learnt integration of Ads Kit in Banking application. It provides developers different capabilities to deliver good quality ads content to users.
Reference
Ads Kit: Documentation

Related

Earthquake application with Huawei Map and Location kits

Learn how to create a earthquake app that includes the Huawei Map Kit and Location Kit using the Flutter SDK.
{
"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"
}
Introduction
Hello everyone, In this article, I will make an earthquake application with Flutter. We will make an some APIs provided by the Huawei Map and Location Kit and learn how to use them in an earthquake project.
Huawei Map Kit, provides standard maps as well as UI elements such as markers, shapes, and layers for you to customize maps that better meet service scenarios.
Huawei Location Kit combines the GPS, Wi-Fi, and base station locations to help you quickly obtain precise user locations, build up global positioning capabilities, and reach a wide range of users around the globe.
Configuring The Project
Before you get started, you must register as a HUAWEI developer and complete identity verification on the HUAWEI Developer website. For details, please refer to Register a HUAWEI ID.
Create an app in your project is required in App Gallery Connect in order to communicate with Huawei services.
★ Integrating Your Apps With Huawei HMS Core
Permissions
In order to make your kits work perfectly, you need to add the permissions below in AndroidManifest.xml file.
Code:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
HMS Integration
Check whether the agconnect-services.json file and signature file are successfully added to the android/app directory of the Flutter project.
Add the Maven repository address and AppGallery Connect service dependencies into the android/build.gradle file.
Code:
buildscript {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
/*
* <Other dependencies>
*/
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}
Configure the Maven repository address for the HMS Core SDK in allprojects.
Code:
allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
Add the apply plugin: ‘com.huawei.agconnect’ line after the apply from: “$flutterRoot/packages/flutter_tools/gradle/flutter.gradle” line.
Code:
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.huawei.agconnect'
Open your app level build.gradle (android/app/build.gradle) file and set minSdkVersion to 19 or higher in defaultConfig. (applicationId must match the package_name entry in the agconnect-services.json file)
Code:
defaultConfig {
applicationId "<package_name>"
minSdkVersion 19
/*
* <Other configurations>
*/
}
Creating Flutter Application
Find your Flutter project’s pubspec.yaml file directory open it and add Huawei Map and Huawei Location Plugins as a dependency. Run the flutter pub get command to integrate the Map & Location Plugins into your project.
There are all plugins in pub.dev with the latest versions. If you have downloaded the package from pub.dev, specify the package in your pubspec.yaml file. Then, run flutter pub get command.
Map Kit Plugin for Flutter
Location Kit Plugin for Flutter
Code:
dependencies:
flutter:
sdk: flutter
http: ^0.12.2
huawei_map: ^4.0.4+300
huawei_location: ^5.0.0+301
We’re done with the integration part! Now, we are ready to make our earthquake application ✓
Make an Earthquake App with Flutter
Create Earthquake Response Data
First of all, we will create an EartquakeResponseData class to access the latest earthquake data. Later, we will call this class with the getData() function.
Additionally, we will use the http package to access the data, so we need to import import ‘package: http / http.dart’ as http; into the detail page.dart class.
More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0204423826967890020
Could be useful for some countries.
Very very useful.
Very interesting.
Where are you getting the data for earthQuake and in what format ?

Huawei ML Kit - Image Classification feat. React Native(Cross Platform)

Introduction
{
"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"
}
Image Classification is the process where a computer device can analyse and define the image category or class in simple language.
Image Classification can be used in many intelligent services, photo gallery application and to design highly efficient security products and services.
Huawei ML Kit enable developers to use its highly efficient API’s to implement the image classification for their mobile applications.
Huawei ML Kit classifies the elements presents in an image into defined categories such as objects, environment, artwork, animal, etc.
Huawei ML Kit’s Image Classification API’s are available for two different classification modes
On Device
Device based classification API’s analyse and detects the image category by running the on device algorithm and supports over 400 different image categories.
On Cloud
Cloud based classification API’s analyse and detects the image category by running the on cloud algorithm and supports over 12000 different image categories.
Development Overview
We will be focusing on simple Image Classification service from an image using on device API’s
Prerequisite
1. Must have a Huawei Developer Account
2. Must have a Huawei phone with HMS 4.0.0.300 or later
3. React Native environment with Android Studio, Node Js and Visual Studio code.
Major Dependencies
1. React Native CLI : 2.0.1
2. Gradle Version: 6.0.1
3. Gradle Plugin Version: 4.0.1
4. React Native ML Kit SDK : 5.0.0
5. react-native-hms-ml kit gradle dependency
6. AGCP gradle dependency
Software Requirements
1. Java SDK 1.8 or later
2. Android 5.0 or later
Preparation
1. Create a react native project using React Native CLI and open android directory.
2. Download the React Native ML Kit SDK and paste it under Node modules directory of React Native project.
3. Create an app and project in the Huawei AppGallery Connect.
4. Provide the SHA Key and App Package name of the project.
5. Enable the ML API and download the agconnect-services.json file and paste to the app folder of your android folder.
Integration
Add below to build.gradle (project) file, under buildscript/repositories and allprojects/repositories.
Maven {url 'http://developer.huawei.com/repo/'}
Add below to build.gradle (app) file, under dependencies to use the ML kit SDK in your React Native application.
Code:
dependencies{
// Import the SDK.
implementation project(":react-native-hms-ml ")
…
…
implementation 'com.huawei.agconnect:agconnect-core:1.2.1.301'
}
Add below under Settings.Gradle file
Code:
include ':react-native-hms-ml
project(':react-native-hms-ml).projectDir = new File(rootProject.projectDir, '../node_modules/react-native-hms-ml /android')
Add below under MainApplication.java file.
Code:
import com.huawei.hms.rn.ml.HmsMlPackage;
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
//Add following line. Don't forget to add import statement also.
packages.add(new HmsMlPackage());
return packages;
}
...
};
Add below under AndroidManifest.xml file
Code:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value="ocr"/>
</application>
Use case
Huawei ML kit’s Image Classification API provides many different use cases, however we will take very simple gallery image classification as example.
Imageclassification.js
Imageclassification.js is a java script file which we will keep under “src” folder of the project to have the navigation stack from the launch page.
We will re-direct to “Classify” tab from here to showcase the image classification capabilities.
More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0203430657683920058

Beginner: Notification Alert! Integration of Huawei Push Kit in Application (Kotlin)

{
"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"
}
Introduction
In this article, I will be integrating Huawei Push Kit in an Application.
Push Kit
Push notifications offers a great way to increase your application’s user engagement and boost your retention rates by sending meaningful messages or by informing users about your application. Push Kit supports two types of messages: notification messages and data messages. You can send notifications and data messages to your users from your server using the Push Kit APIs or directly from the AppGallery Push Kit Console.
Requirements
1. Any operating system (MacOS, Linux and Windows).
2. Must have a Huawei phone with HMS 4.0.2.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
4. Minimum API Level 21 is required.
5. Required EMUI 9.0.0 and later version devices.
Integrate HMS Dependencies
1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
2. Create a project in android studio, refer Creating an Android Studio Project.
3. Generate a SHA-256 certificate fingerprint.
4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in SHA256 CODE
5. Create an App in AppGallery Connect.
6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory.
7. Enter SHA-256 certificate fingerprint and click Save.
8. Click Manage APIs tab and enable Push Kit.
9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
Code:
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
10. Add the below plugin and dependencies in build.gradle(Module) file.
Code:
apply plugin: id 'com.huawei.agconnect'
//Huawei AGC
Code:
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Push Kit
Code:
implementation 'com.huawei.hms:push:6.3.0.302'
11. Now Sync the gradle.
12. Add the required permission to the Manifestfile.xml file.
Code:
// Push Kit
<uses-permission android:name="android.permission.INTERNET" />
<service
android:name=".PushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
Sometimes adding everything manually can be burden. Let’s use Configuration Wizard for the setup.
Configuration Wizard
Now, we will help how to use configuration wizard of HMS Toolkit to automatically complete necessary configuration for development preparations.
Watch this video to understand how to use the configuration Wizard.
Once all the items are passed, you are good to go.
Integrating Push Kit Through Coding Assistant
Navigate to HMS > Coding Assistant, and select Push Kit in the kit list. The following figure shows all the scenarios of Push Kit.
Completing the Code by Dragging and Dropping Scenario Cards.
Select a scenario card for development, for example, Send notification message-Open home pages of corresponding apps, and drag and drop the card to the code area to generate the code for obtaining a push token
Using a Tool to Send Messages
On the toolbar of the Push Kit card, select Message Test to test whether the server can push messages to the device.
Click Get APP ID and APP SECRET. The tool automatically reads the app ID and app secret from AppGallery Connect. Click Next.
On the page that is displayed, set parameters as prompted or based on the following table.
After the setting, click Send.
Result
After successful message sending, you will receive a notification message on your device.
Tips and Tricks
1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
2. Make sure you have added the agconnect-services.json file to app folder.
3. Make sure you have added SHA-256 fingerprint without fail.
4. Make sure all the dependencies are added properly.
Conclusion
In this article, we have learnt integration of Push Kit in application. It will guide you to increase your application’s user engagement and boost your retention rates by sending meaningful messages or by informing users about your application.
Reference
Push Kit: Documentation
Push Kit: Training Video

Beginner: Guideline to Integrate Huawei Mobile Services Account Kit using React Native

{
"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"
}
Introduction
In this article, we will be integrating Huawei Account Kit in an Application.
Account Kit
Account Kit: Account Kit provides you with simple, secure and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.
React Native
REACT Native helps you create real and exciting mobile apps with the help of JavaScript only, which is supportable for both android and iOS platforms.
Just code once, and the REACT Native apps are available for both iOS and Android platforms which helps to save development time.
React Native is a framework that builds a hierarchy of UI components to build the JavaScript code.
It uses the same fundamental UI building blocks as regular iOS and Android apps.
Requirements
Any operating system (MacOS, Linux and Windows).
Must have a Huawei phone with HMS 4.0.2.300 or later.
Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
Minimum API Level 21 is required.
Required EMUI 9.0.0 and later version devices.
Integrate HMS Dependencies
First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
Create a project in android studio, refer Creating an Android Studio Project.
Generate a SHA-256 certificate fingerprint.
To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code or use cmd as explained in SHA256 CODE
Create an App in AppGallery Connect.
Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
Enter SHA-256 certificate fingerprint and click Save, as follows.
Click Manage APIs tab and enable Account Kit.
React Native Project Preparation
Environment set up, refer below link.
Setting up the development environment · React Native
This page will help you install and build your first React Native app.
reactnative.dev
Create project using below command.
Code:
react-native init project name
Download the React Native Account Kit SDK and paste it under Node Modules directory of React Native project. If you cannot find node modules run below command under project directory using CLI.
Code:
“npm install” & “npm link”
Configure android level build.gradle
Add to buildscript/repositories and allprojects/repositories
Code:
maven {url 'http://developer.huawei.com/repo/'}
Configure app level build.gradle. (Add to dependencies)
Code:
Implementation project (“:react-native-hms-account”)
Linking the HMS Account Kit Sdk.
Run below command in the project directory
Code:
react-native link react-native-hms-account
Add below permissions to Android.manifest file.
Code:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Development
Account Kit provides APIs for normal sign-in, silent sign-in, sign-out, and authorization revoking. Create a UI in your Android Studio project. An example is shown in the following figure.
1. signIn()
Sign In securely to the application using Huawei ID.
JavaScript:
import HMSAccount, {
HMSAuthParamConstants,
HMSAuthRequestOptionConstants,
} from "react-native-hms-account";
let signInData = {
huaweiIdAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
authRequestOption: [
HMSAuthRequestOptionConstants.ID_TOKEN,
HMSAuthRequestOptionConstants.EMAIL,
],
};
HMSAccount.signIn(signInData)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log(err);
});
2. signOut()
Signing out securely using the signOut() method.
JavaScript:
HMSAccount.signOut()
.then(() => {
console.log("signOut -> Success")
})
.catch((err) => {
console.log(err)
});
3. silentSignIn()
With silent sign-in, users can sign in without using their credentials for consecutive sign-ins
JavaScript:
import HMSAccount, { HMSAuthParamConstants } from "react-native-hms-account";
let silentSignInData = {
huaweiIdAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
};
HMSAccount.silentSignIn(silentSignInData)
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err)
});
4. cancelAuthorization()
Revoking authorization is intended to increase security by forcing the users to use sign-in credentials while signing in.
JavaScript:
HMSAccount.cancelAuthorization()
.then(() => {
console.log("cancelAuthorization -> Success")
})
.catch((err) => {
console.log(err)
});
5. HMSReadSMSManager
Read SMS manager is a service that listens to verification code SMS events.
JavaScript:
import { HMSReadSMSManager } from "react-native-hms-account";
HMSReadSMSManager.smsVerificationCode()
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err)
});
Testing
Run the android App using the below command.
Code:
react-native run-android
Result
Conclusion
In this article, we have learnt that how to integrate HMS Account Kit using React Native. HMS Toolkit helps us to Connects users from a wide range of devices, including phones, tablets, and smart displays. It serves over 900 million users from 190+ countries and regions worldwide and supports 70+ Languages. It guarantees two-factor authentication, real-time risk prediction, and GDPR compliance.
Reference
Account Kit: Documentation
Account Kit: Training Video

Guideline to Integrate Huawei ID button to Sign in (Account Kit) using React Native

{
"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"
}
Introduction
In this article, I will be integrating Huawei Account Kit in a Travel application. Account Kit provides you with simple, secure and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.
React Native
REACT Native helps you create real and exciting mobile apps with the help of JavaScript only, which is supportable for both android and iOS platforms.
Just code once, and the REACT Native apps are available for both iOS and Android platforms which helps to save development time.
React Native is a framework that builds a hierarchy of UI components to build the JavaScript code.
It uses the same fundamental UI building blocks as regular iOS and Android apps.
Requirements
1. Any operating system (MacOS, Linux and Windows).
2. Must have a Huawei phone with HMS 4.0.2.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
4. Minimum API Level 21 is required.
5. Required EMUI 9.0.0 and later version devices.
Integrate HMS Dependencies
1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
2. Create a project in android studio, refer Creating an Android Studio Project.
3. Generate a SHA-256 certificate fingerprint.
4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in SHA256 CODE
5. Create an App in AppGallery Connect.
6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
7. Enter SHA-256 certificate fingerprint and click Save, as follows.
8. Click Manage APIs tab and enable Account Kit.
React Native Project Preparation
9. Environment set up, refer below link.
Setting up the development environment · React Native
This page will help you install and build your first React Native app.
reactnative.dev
10. Create project using below command.
Code:
react-native init project name
11. Download the React Native Account Kit SDK and paste it under Node Modules directory of React Native project. If you cannot find node modules run below command under project directory using CLI.
Code:
“npm install” & “npm link”
12. Configure android level build.gradle
Code:
Add to buildscript/repositories and allprojects/repositories
maven {url 'http://developer.huawei.com/repo/'}
13. Configure app level build.gradle. (Add to dependencies)
Code:
Implementation project (“:react-native-hms-account”)
14. Linking the HMS Account Kit Sdk.
Run below command in the project directory
Code:
react-native link react-native-hms-account
15. Add below permissions to Android.manifest file.
XML:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Development
Sign in and sign out (Without Auth Button)
signIn()
Sign In securely to the application using Huawei ID.
JavaScript:
import HMSAccount, {
HMSAuthParamConstants,
HMSAuthRequestOptionConstants,
} from "react-native-hms-account";
let signInData = {
huaweiIdAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
authRequestOption: [
HMSAuthRequestOptionConstants.ID_TOKEN,
HMSAuthRequestOptionConstants.EMAIL,
],
};
HMSAccount.signIn(signInData)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log(err);
});
Sign Out
JavaScript:
HMSAccount.signOut()
.then(() => {
console.log("signOut -> Success")
})
.catch((err) => {
console.log(err)
});
Sign in using HMSAuthButton
JavaScript:
import HMSAccount, { HMSAccountAuthService, HMSAuthParamConstants, HMSAuthScopeListConstants, HMSAuthRequestOptionConstants } from "@hmscore/react-native-hms-account";
signIn = () => {
let signInData = {
accountAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
authRequestOption: [HMSAuthRequestOptionConstants.ID_TOKEN, HMSAuthRequestOptionConstants.ACCESS_TOKEN],
authScopeList: [HMSAuthScopeListConstants.EMAIL]
};
HMSAccountAuthService.signIn(signInData)
.then((response) => { console.log(response) })
.catch((err) => { console.log(err) });
};
...
<HMSAuthButton
style={styles.viewcontainer}
colorPolicy={
HMSAccount.HUAWEI_ID_AUTH_BUTTON_COLOR_POLICY_RED
}
enabled={true}
theme={HMSAccount.HUAWEI_ID_AUTH_BUTTON_THEME_FULL_TITLE}
cornerRadius={
HMSAccount.HUAWEI_ID_AUTH_BUTTON_CORNER_RADIUS_MEDIUM
}
onPress={signIn}
/>
Testing
Run the android App using the below command.
react-native run-android
Result
Tips and Tricks
1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
2. Make sure you have added the agconnect-services.json file to app folder.
3. Make sure you have added SHA-256 fingerprint without fail.
4. Make sure all the dependencies are added properly.
5. For project cleaning navigate to android directory and run the below command.
gradlew clean
Conclusion
In this article, we have learnt that how to integrate HMS Account Kit using React Native. HMS Toolkit helps us to Connects users from a wide range of devices, including phones, tablets, and smart displays. It serves over 900 million users from 190+ countries and regions worldwide and supports 70+ Languages. It guarantees two-factor authentication, real-time risk prediction, and GDPR compliance.
Reference
Account Kit: Documentation
Account Kit: Training Video

Categories

Resources