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

{
"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

Related

[Push Kit Practice] Recommending Scenario-specific Content for App Users

More information like this, you can visit HUAWEI Developer Forum​
The capability to push messages to users is especially important for improving app engagement. However, it may be difficult to know what users want using conventional push methods, which do not tell you about the status of a user, and as a result, your push messages may fail to attract the attention of users or, even worse, may simply annoy them.
Wouldn't it be good if there was a way to learn about the status of a user in order to push custom content?
HUAWEI Push Kit 5.0 provides scenario-based messaging, which may help you address this pain point. The function identifies user status and displays content that users are interested in at the proper time and in the proper scenarios.
The following uses a demo to describe how to quickly push messages based on specific scenarios. This demo is open source and the source code can be downloaded from
https://github.com/HMS-Core/hms-push-clientdemo-android
1. Development Practice
1. Integrating the HMS Core Push SDK 5.0
1.1 Getting Started
1.1.1 Adding the AppGallery Connect Configuration File of Your App
Download the agconnect-services.json file.
{
"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"
}
Move the downloaded agconnect-services.json file to the app directory of your Android Studio project.
1.2 Configuring the Maven Repository Address for the HMS Core SDK
Open the build.gradle file in the root directory of your Android Studio project.
Add the AppGallery Connect plugin and the Maven repository.
Code:
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
1.1.3 Adding Build Dependencies
Open the build.gradle file in the app directory.
Add build dependencies in the dependencies section.
Code:
dependencies {
implementation 'com.huawei.hms:push:5.0.0.300'
}
Add the following information under apply plugin: 'com.android.application' in the file header:
Code:
apply plugin: 'com.huawei.agconnect'
1.1.4 Configuring the AndroidManifest.xml File
Define the DemoHmsMessageService class (the class name is only for reference and can be customized) by inheriting from HmsMessageService, and implement its methods.
Code:
android
:name=
".DemoHmsMessageService"
android
:exported=
"false"
>
android
:name=
"com.huawei.push.action.MESSAGING_EVENT"
/>
1.1.5 Configuring Obfuscation Scripts
Open the obfuscation configuration file proguard-rules.pro of your project.
Code:
-ignorewarning -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}
The preceding describes key integration steps. For more details, please refer to the Push SDK 5.0 development guide, which you can access from the following link:
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-config-agc-0000001050170137
Ensure that Push Kit is available before going to the next step.
2. Scenario-based Messaging
The scenarios include:
1. Holidays and weekends
2. Temperature (hot, comfortable, or cold)
3. UV intensity
4. DND canceled
5. Headset inserted or Bluetooth car kit disconnected
After completing the basic integration, you only need to add four lines of code and apply for the relevant user permission to implement scenario-based messaging.
· Apply for location permission in the holiday/weekend, UV intensity, and temperature scenarios. In addition, enable location permission for HMS Core and your app.
Code:
android
:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
android
:name=
"android.permission.ACCESS_BACKGROUND_LOCATION"
/>
android
:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
· Apply for Bluetooth permission for headset inserted or Bluetooth car kit disconnected.
Code:
android
:name=
"android.permission.BLUETOOTH"
/>
The preceding operations may involve applying for new user permission. You may therefore need to update the privacy statement of your app and add usage description for the new user permission.
The HMS Core and Push Service version requirements for scenario-based messaging are as follows.
You'll need to perform testing on devices that satisfy the above version requirements.
Once you've completed all of the above, you are now ready to create a task on the Push Kit console.
3. Operations on the Push Kit Console
Step 1: Sign in to AppGallery Connect, select My projects, find your project from the project list, and click the app for which you need to create a task.
Step 2: Go to Project Setting > Growing > Push Kit > Automated notifications(Beta) and click New scenario push task to create a push task.
Step 3: Set relevant parameters.
Step 4: Select a scenario.
Step 5: Click Submit.
After the task is created, the task status changes to Sent, indicating that the task has been delivered to the device.
It takes about 10 minutes to create a scenario-based task. When a scenario triggering condition is met, the message will be displayed on the user device.
4. Display Effect
In the screenshot above, targeted messaging is implemented based on the headset inserted or Bluetooth car kit disconnected scenario. You are welcome to explore more messaging scenarios to ensure you send your users the proper messages at the proper time and in the proper scenarios.
For more information about HUAWEI Push Kit, visit
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060
It's really worth trying

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.

HMS Multi Kit Integration (Extended) in Unity Game Development

Introduction
Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will be integrating following kits:
Ads Kit
Game services
Analytics Kit
Location Kit
Push Kit
We will learn to integrate above HMS Kits in Unity game development using official plugin. And by integrating in single application gives experience the ease of development and give best user experience and showcases stability of the kits, and how we can use kits efficiently to make users experience the best of it.
Development Overview
You need to install Unity software and I assume that you have prior knowledge about the unity and C#.
Hardware Requirements
A computer (desktop or laptop) running Windows 10.
A Huawei phone (with the USB cable), which is used for debugging.
Software Requirements
Java JDK installation package.
Unity software installed.
Visual Studio/Code installed.
HMS Core (APK) 4.X or later.
Integration Preparations
1. Create a project in AppGallery Connect.
2. Create Unity project.
{
"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"
}
3. Adding Huawei HMS AGC Services to project.
5. Generate a SHA-256 certificate fingerprint.
To generating SHA-256 certificate fingerprint use below command.
6. Configure the signing certificate fingerprint.
7. Download and save the configuration file.
Add the agconnect-services.json file following dir Assets > Plugins > Android
8. Add the following plugin and dependencies in LaucherTemplate
Code:
apply plugin: 'com.huawei.agconnect'
Code:
implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
implementation 'com.huawei.hms:base:5.0.0.301'
implementation 'com.huawei.hms:hwid:5.0.3.301'
implementation 'com.huawei.hms:game:5.0.1.302'
implementation 'com.huawei.hms:push:4.0.1.300'
implementation 'com.huawei.hms:hianalytics:5.0.3.300'
9. Add the following dependencies in MainTemplate.
Code:
implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
implementation 'com.huawei.hms:base:5.0.0.301'
implementation 'com.huawei.hms:hwid:5.0.3.301'
implementation 'com.huawei.hms:game:5.0.1.302'
implementation 'com.huawei.hms:hianalytics:5.0.3.300'
implementation 'com.huawei.hms:ads-lite:13.4.29.303'
implementation 'com.huawei.hms:ads-consent:3.4.30.301'
implementation 'com.huawei.hms:push:4.0.1.300'
implementation 'com.huawei.hms:location:5.0.0.301'
10. Add the following dependencies in MainTemplate.
repositories & class path in BaseProjectTemplate.
Code:
maven { url 'https://developer.huawei.com/repo/' }
Code:
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
11. Add Achievement details in AGC > My apps
12. Add LeaderBoard details.
Continue reading..
Tips and Tricks
Add agconnect-services.json file without fail.
Add SHA-256 fingerprint without fail.
Add Achievements and LeaderBoad details before run.
Make sure dependencies added in build files.
Enable location and accept permission to read location.
Conclusion
We have learnt integration of HMS GameService Kit, Ads Kit,Location kit, Push Kit and Analytics kit in Unity. Error code while fetching Player extra information and Event Begin and end.
7006: The account has not been registered in Chinese mainland. In this case, perform bypass and no further action is required.
Thanks for reading, please do like and comment your queries or suggestions.
Read full article
References
Hms game services
HMS Ads Kit
HMS Analytics Kit
HMS Push Kit
Location Kit

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

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

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