Development Guide for Integrating HUAWEI Account Kit Using Unity - Huawei Developers

1.1 Service Introduction​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.
1.2 Version Change History​
{
"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"
}
1.2.1 Version Dependencies​l The SDK of the latest version can be used only on devices running HMS Core (APK) 4.0.0.300 or later. If a user is using a device without HMS Core (APK) 4.0.0.300 or later, the user will be directed to install it when it is called by your app. User devices need to run EMUI 3.0 or later or Android 4.4 or later (API level 19 or later). The Unity version must be 2018.4.25 or later.
1.3 Preparations​1.3.1 Importing Unity Assets​1. Open Asset Store.
Go to Window > Asset Store in Unity.
2. Search for the Huawei HMS AGC Services asset. Download and then import it.
3. Import the asset to My Assets, with all services selected.
4. Search for the Unity Distribution Portal (UDP) asset in Asset Store. Download and then import it.
5. Change the package name.
Go to Edit > Project Settings > Player, click the Android icon, and go to Other Settings in Unity. Then, set Package Name.
The default package name is com.${Company Name}.${Product Name}. You need to change the package name, and the app will be released to AppGallery with the new name.
1.3.2 Generating .gradle Files​1. Enable project gradle.
Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Build.
Enable Custom Base Gradle Template.
Enable Custom Launcher Gradle Template.
Enable Custom Main Gradle Template.
Enable Custom Main Manifest.
2. Signature
You can use an existing keystore file or create a new one to sign your app.
Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Keystore Manager > Keystore... > Create New.
Enter the password when you open Unity. Otherwise, you cannot build the APK.
1.3.3 Configuring .gradle Files​1. Configure the baseProjectTemplate.gradle file.
Java:
allprojects {
buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
}
}
repositories {
google()
jcenter()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
maven { url 'https://developer.huawei.com/repo/' }
}
}
2. Configure the launcherTemplate.gradle file.
Java:
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
implementation 'com.huawei.hms:base:4.0.1.300'
implementation 'com.huawei.hms:hwid:4.0.1.300'
...
}
3. Configure the mainTemplate.gradle file.
Java:
apply plugin: 'com.android.library'
apply plugin: 'com.huawei.agconnect'
dependencies {
...
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
implementation 'com.huawei.hms:base:4.0.1.300'
implementation 'com.huawei.hms:hwid:4.0.1.300'
...
}
1.3.4 Adding the agconnect-services.json File​1. Create an app by following instructions in Creating an AppGallery Connect Project and Adding an App to the Project.
Run keytool -list -v -keystore C:\TestApp.keyStore to generate the SHA-256 certificate fingerprint based on the keystore file of the app. Then, configure the fingerprint in AppGallery Connect.
2. Download the agconnect-services.json file and place it in the Assets/Plugins/Android directory of your Unity project.
1.3.5 Enabling Account Kit​1. Sign in to AppGallery Connect.
2. Click My projects and find your project. Go to Build > Account Kit.
1.4 App Development​For details, please refer to udpServiceSampleScript under Assets > HuaweiServiceDemo > Scripts in the demo package provided by Huawei.
1.4.1 Uploading Your Project to UDP​For details about UDP, please refer to its official documents.
1.
Go to Window > Unity Distribution Portal > Settings in Unity and click Go to the Services Window.
2. Select an organization.
3. Link your project to a UDP client.
Go to Window > Unity Distribution Portal > Settings > Inspector in Unity and click Generate new UDP client.
4. Set Game Title and UDP Sandbox Test Accounts.
5. Push all the settings.
1.4.2 Testing the APK​1. Go to File > Build Settings > Android in Unity and click Switch Platform.
1.1 Select UDP/Sample/UDPSampleScene.
1.2 Build the project. Save and install the APK.
2. Use the configured sandbox test account to initialize the APK.
2.1 Open the APK and tap Init.
2.2 Enter the configured sandbox account.
2.3 Verify that the sandbox test is successful.
1.4.3 Repacking the APK​1. Sign in to the UDP console.
2. Click the newly created game.
3. Edit the game information and then click Release.
4. Choose HUAWEI AppGallery.
5. Set your In-App Purchases public key manually. Unity will automatically obtain other information from AppGallery Connect based on the package name.
6. Choose HUAWEI AppGallery and click Publish.
7. The UDP console repacks your APK.
8. Download the repacked APK, which supports sign-in with a HUAWEI ID.
More Information
To join in on developer discussion forums, go to Reddit
To download the demo app and sample code, go to GitHub
For solutions to integration-related issues, go to Stack Overflow
Read In Forum

Related

An Introduction of HMS In-App Purchase in Ionic Application ( Cross Platform Integration )

Introduction
Back in the 90’s, it was a revolutionary to have your own website and putting an advertisement in the website was the only source of revenue. But today as space matures, it’s essential for brands to have a clear strategy to monetize their mobile presence. Some companies charge their users before downloading their apps. Others depend on in-app advertising as their sole revenue stream. But, for a lot of brands, their mobile monetization strategy relies entirely (or some part) on In App Purchase. To summarise, when a customer spends money within a mobile app, that’s an in-app purchase.
To make easier for users to make payments and developers to focus solely on app innovation, Huawei provided us with an extraordinary kit i.e. HMS Core In App Purchase (IAP) Kit. HMS In App Purchase helps developers in variety of situation to earn revenue such as paying for access to a dating app’s special features, subscribing to a streaming music app’s premium tier, buying more gold bars in games etc.
In this article, we are going to integrate HMS Core In App Purchase Kit into an Ionic application.
Ionic Framework
Ionic Framework is an open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies such as HTML, CSS, and JavaScript with integrations for popular frameworks like Angular and React.
Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling. Unlike a responsive framework, Ionic comes with very native-styled mobile UI elements and layouts that you’d get with a native SDK on Android or iOS but didn’t really exist before on the web.
Since Ionic is an HTML5 framework, it needs a native wrapper like Cordova or Capacitor in order to run as a native app.
Here we will use Ionic framework with Angular and Capacitor as native wrapper.
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"
}
The above Ionic framework demo application is created with the same concept of my previous work and that is a quiz game which was created using android native and java as language. The concept of this game is to show In App Purchase in a game and how user can purchase subscription and non-subscription products. The name of the game is Quiz Mania. Below is the demo of that application.
To know more click on the link below:
https://forums.developer.huawei.com/forumPortal/en/topic/0201255229704010231
Prerequisite
1) Must have a Huawei Developer Account.
2) Must have a Huawei phone with HMS 4.0.0.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) Must install node in the system
5) Must install Ionic in the system using below command:
Code:
npm install -g @ionic/cli
Things need to be done
1) Generating a Signing Certificate Fingerprint. For generating the SHA key, refer this article.
2) Create an app in the Huawei AppGallery connect and enable In App Purchase Kit in Manage Api section.
3) Provide the SHA Key in App Information Section.
4) Provide storage location.
5) Download the agconnect-services.json and store it somewhere on our computer.
6) Create a blank Ionic Project using below command:
Code:
ionic start Your_Application_Name blank --type=angular
7) Run the following command in the root directory of your Ionic project to install HMS IAP kit it through npm.
Code:
npm install @hmscore/cordova-plugin-hms-iap
8) If you want full Ionic support with code completion etc., install @iONiC-native/core in your project.
Code:
npm install @ionic-native/core --save-dev
9) Run the following command to copy the "ionic/dist/hms-ml" folder from library to "node_modules/@iONiC-native" folder under your Ionic project.
Code:
cp node_modules/@hmscore/cordova-plugin-hms-iap/ionic/dist/hms-in-app-purchases node_modules/@ionic-native/ -r
10) Run the following command to compile the project:
Code:
ionic build
npx cap init [appName] [appId]
Where appName is the name of your app, and appId is package_name in your agconnect-services.json file (ex: com.example.app).
11) Run the following command to add android platform to your project:
Code:
ionic capacitor add android
12) Make sure your project has a build.gradle file with a maven repository address and agconnect service dependencies as shown below:
Code:
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.google.gms:google-services:4.3.3'
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply from: "variables.gradle"
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
13) Add the Signing certificate configuration to the build.gradle file in the app directory as show below:
Code:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "YOUR_PACKAGE_NAME"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file("mykeystore.jks") // Signing certificate.
storePassword "XXXXXX" // KeyStore password.
keyAlias "XXXXXX" // Alias.
keyPassword "XXXXXX" // Key password.
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
debuggable true
}
}
}
14) Add plugin to the build.gradle file in the app directory as show below:
Code:
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
15) Add site kit service implementation into to dependencies section of build.gradle file in the app directory as show below:
Code:
dependencies {
….
implementation 'com.huawei.hms:iap:5.0.4.301'
}
16) Add agconnect-services.json and signing certificate jks file to the app directory in your Android project as show below:
17) To update dependencies, and copy any web assets to your project, run the following code:
Code:
npx capacitor sync
More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0204418699662160038
​

How Can I Quickly Integrate App Linking of AppGallery Connect into an Unity-based App?

How Can I Quickly Integrate App Linking of AppGallery Connect into an Unity-based App?
Recently, my app needs to use cross-platform sharing links. App Linking of AppGallery Connect just meets my requirements.
Perform the following steps for service integration:
Step 1: Create an app, enable App Linking, and create a URL prefix.
Step 2: Integrate the App Linking SDK into a Unity project.
Step 3: Import the HuaweiService package to Unity Editor.
Step 4: Create a link of App Linking.
Step 5: Receive and test the link.
1. Enable App Linking and create a URL prefix.
1. Create an app or use an existing app in AppGallery Connect. Click My projects and go to Grow > App Linking, and click Enable now in the displayed page.
{
"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"
}
2. On the displayed App Linking service page, click the URL prefix tab and then click Add URL prefix to create a unique URL prefix.
The system will automatically check your domain name to ensure its uniqueness.
2. Integrate the App Linking SDK into a Unity project.
Prepare the Android development environment in Unity Editor.
1. Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select items for Android as required.
Note: You need to add build dependencies for Android in Unity Hub, as shown in the following figure.
2. In AppGallery Connect, click My projects, and find your project from the list. Go to Project settings > General information, download the agconnect-services.json file, and save the file to the Assets\Plugins\Android directory of your Unity project.
3. In Unity Editor, choose Edit > Project Settings > Player > Other Settings, and set the package name to the one you set in AppGallery Connect.
4. Add the following code to the project-level baseProjectTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
5. Add the following code to the app-level LauncherTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
dependencies {
implementation 'com.huawei.agconnect:agconnect-applinking:1.4.1.300'
}
3. Import the HuaweiService package to Unity Editor.
1. Import the source package to your project. You can either download the package from the Unity Asset Store, or download it from the following website:
https://share.unity.com/receive/?th...e=CHA6BzFizOIgjWvhSXYRaJiNzOcwfItJRY7dZSNEb4A
2. After the download is complete, choose Assets > Import package, select the required package, and click Import.
4. Create a link of App Linking.
You can create a link of App Linking in AppGallery Connect or by calling an API in your Unity project. Let’s see how to create a link in your Unity project.
1. Create buttons.
In Unity Editor, choose GameObject > UI > Button, and create two buttons called create and share. Click Add Component on the right to create a script file.
2. Add a method for creating a link.
Add the following method to the script file of the test app:
Java:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiService.AppLinking;
using HuaweiService;
public class testApp : MonoBehaviour
{
private const string URI_PREFIX = "https://testcocos1203.drcn.agconnect.link";
private const string DEEP_LINK = "https://developer.huawei.com/consumer/cn/service/josp/agc/index.html";
void Start()
{
}
void Update()
{
}
public void createLongLink() {
AGConnectAppLinking.getInstance();
AppLinking.Builder builder = createBuilder();
Uri applinkingUri = builder.buildAppLinking().getUri();
var link = applinkingUri.toString();
Debug.Log ("AppLinking create" + link);
GUIUtility.systemCopyBuffer = link;
}
public AppLinking.Builder createBuilder(){
return AppLinking.newBuilder()
.setUriPrefix(URI_PREFIX)
.setDeepLink(Uri.parse(DEEP_LINK))
.setAndroidLinkInfo(
AppLinking.AndroidLinkInfo.newBuilder()
.build());
}
public void OpenLink()
{
var activity = new Activity();
var intent = new Intent();
intent.setData(Uri.parse(GUIUtility.systemCopyBuffer));
activity.startActivity(intent);
}
}
3. Bind a method to a button.
In Unity Editor, click the create button. In the On Click() area on the right, add a tap event, as shown in the following figures.
Bind a method to the share button as well.
Note: You can drag the .cs file created by clicking create to the Add Component area on the right to add a tap event.
5. Receive a link.
To receive a link, you need to configure the manifest file and set the getAppLinking method.
1、 Configure the manifest file. Here, the scheme of the deep link domain name is set in the file.
Example: DEEP_LINK = ‘https://developer.huawei.com/cn/';
Sample code:
6. Package your app and test your link.
1. After your app runs properly, tap create to create a link. Then tap share to open the link in a browser.
2. If the app can be launched, the link is normal.
7. Summary
The integration is simple. The SDK is small, and links can be shared across Android and iOS. No adaptation is required on different platforms, reducing the workload.
Operations personnel can create links in AppGallery Connect for app promotion, and you can write code in apps to create links, which is very convenient.
Reference:
HUAWEI AppGallery Connect App Linking documentation: https://developer.huawei.com/consum...ry-connect-Guides/agc-applinking-introduction
Unity documentation about App Linking: https://docs.unity.cn/cn/Packages-c...linking.html#integrating-huawei-analytics-kit
Unity Editor Installation and APK Packaging: https://forums.developer.huawei.com/forumPortal/en/topic/0204435788744370088?fid=0101187876626530001
if you have any questions,please refer to stackoverflow .
Thanks.
Ganyo1 said:
Thanks.
Click to expand...
Click to collapse
Thank you for your like. You are welcome.

How Can I Quickly Integrate AppGallery Connect APM into a Unity App?

When an app is used, such problems may occur: slow app launch, Application Not Responding (ANR), app crash, and network loading failure. These are the major issues that affect user experience.
To meet increasing demands of diagnosing performance problems, more and more app performance monitoring services have emerged in the market. HUAWEI AppGallery Connect provides full-process quality services in app development, testing, release, and analysis.If you want to quickly experience this service, see the demo on GitHub.
1. HUAWEI AppGallery Connect APM
App Performance Management (APM) is one of the quality services provided by AppGallery Connect. This service can monitor app performance at the minute level, and is totally free of charge. It does its job by:
Collecting data about app launches, app screen rendering, network requests, and foreground/background activities automatically.
Monitoring ANR problems and recording relevant device information and log information when they occur.
Providing app performance data analysis reports for app optimizations.
Supporting custom traces to monitor app performance data in specific scenarios.
AppGallery Connect APM has the following edges over other app performance monitoring platforms:
Easy integration: You can integrate APM for app performance analysis without any coding.
Comprehensive metrics: APM illustrates an app’s performance in a myriad of dimensions such as app launches, ANR, screen rendering, and network requests, and also supports custom traces, indicators, and dimensions to provide a tailored report for your specific needs.
2. Integrating AppGallery Connect APM
As described in Huawei document, you can easily complete the integration of the service by adding the required plug-in and SDK configurations to your code without any coding. There are just a few simple steps:
i. Create an app in AppGallery Connect and enable APM.
ii. Download and add the JSON file.
iii. Import the HuaweiService package to Unity Editor.
iv. Integrate the APM plug-in and the APM SDK.
Then, you can package and run your app, and view its performance data later in AppGallery Connect.
2.1 Creating an App in AppGallery Connect and Enabling APM
If you need to enable APM for an existing app, make sure that the app package name in the APK file is the same as that configured in AppGallery Connect when the app is created. Access AppGallery Connect, create an app, and enable APM. Ensure that your app package name is the same as that configured in the APK file.
Then select an app under My projects, go to Quality > APM, and click Enable.
{
"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"
}
2.2 Configuring the Android Environment in Unity Editor and Downloading the JSON File
1. Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select items for Android as required.
2. In Unity Editor, choose Edit > Project Settings > Player > Other Settings, and set the package name to the one you set in AppGallery Connect.
2.3 Importing the HuaweiServices Package
1. Download the package here.
2. Choose Assets > Import package.
3. Select the required package, and click Import.
4. In AppGallery Connect, click My projects, and find your project from the list. Go to Project settings > General information, download the agconnect-services.json file, and save the file to the Assets\Plugins\Android directory of your Unity project.
2.4 Integrating the APM SDK into Your Unity Project
1. Add the following code to the project-level baseProjectTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.4.2.301'
classpath ''com.huawei.agconnect:agconnect-apms-plugin:1.4.1.302'
**BUILD_SCRIPT_DEPS**
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
2. Add the following code to the app-level LauncherTmeplate.gradle file in the Assets\Plugins\Android directory:
XML:
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
apply plugin: 'com.huawei.agconnect.apms'
dependencies {
implementation project(':unityLibrary')
implementation 'com.huawei.agconnect:agconnect-apms:1.4.1.303'
}
3. Configure the manifest file.
3. Packaging and Testing
Package the APK file and launch the app. Check whether the corresponding APMS logs are generated.
If so, you have integrated the APM service successfully.
For details, visit: https://forums.developer.huawei.com/forumPortal/en/topic/0203396636080560050?fid=0101188387844930001
4. Summary
In only 4 steps, you can integrate the HUAWEI AppGallery Connect APM SDK without coding, to implement comprehensive app performance monitoring.
The APM analysis report provides detailed device, log, and performance information recorded when an issue occurs. This real-time report drives app operations based on data and provides abundant information for app optimizations. App R&D and operations personnel no longer need to spend much time on locating and reproducing performance problems.
For more details, please check:
AppGallery Connect APM development guide:
https://developer.huawei.com/consum...pGallery-connect-Guides/agc-apms-introduction
Unity documentation about APM:
https://docs.unity.cn/cn/Packages-cn/[email protected]/manual/apm.html
Coding-free Integration of AppGallery Connect APM on Android Platform:
https://forums.developer.huawei.com/forumPortal/en/topic/0203396636080560050?fid=0101188387844930001
HUAWEI AppGallery Connect APM codelab for Android:
https://developer.huawei.com/consumer/en/codelab/AppGalleryConnectAPM/index.html#0

Integrating HUAWEI Push Kit Using Unity

1.1 Version Change History​
{
"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"
}
1.2 Service Introduction​1.2.1 Overview​This document describes how to integrate Push Kit using the official Unity asset. After the integration, your app can use the services of this Kit on HMS mobile phones.
For details about Push Kit, please visit HUAWEI Developers.
1.2.2 Restrictions​For details, please refer to the development guide.
1.2.3 Supported Unity Versions​
Note: If the version is earlier than 2018.4.25, you can manually import assets.
1.3 Preparations​1.3.1 Importing Unity Assets​1. Open Asset Store in Unity.
Go to Window > Asset Store in Unity.
2. Search for the Huawei HMS AGC Services asset. Download and then import it.
3. Import the asset to My Assets, with all services selected.
4. Change the package name.
Go to Edit > Project Settings > Player > Android > Other Settings in Unity, and then set Package Name.
The default package name is com.${Company Name}.${Product Name}. You need to change the package name, and the app will be released to AppGallery with the new name.
1.3.2 Generating .gradle Files​1. Enable project gradle.
Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Build.
Enable Custom Base Gradle Template.
Enable Custom Launcher Gradle Template.
Enable Custom Main Gradle Template.
Enable Custom Main Manifest.
2. Signature
You can use an existing keystore file or create a new one to sign your app.
Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Keystore Manager > Keystore... > Create New.
Enter the password when you open Unity. Otherwise, you cannot build the APK.
1.3.3 Configuring .gradle Files​1. Configure the BaseProjectTemplate.gradle file.
Configure the Maven repository address.
Code:
<p style="line-height: 1.5em;">buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
// If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity.
// For the Gradle version preinstalled with Unity, please visit https://docs.unity3d.com/Manual/android-gradle-overview.html.
// For the official Gradle and Android Gradle Plugin compatibility table, please visit https://developer.android.com/studio/releases/gradle-plugin#updating-gradle.
// To specify a custom Gradle version in Unity, go do Preferences > External Tools, deselect Gradle Installed with Unity (recommended) and specify a path to a custom Gradle version.
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
**BUILD_SCRIPT_DEPS**
}
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
}</p>
2. Configure the launcherTemplate.gradle file.
Code:
<p style="line-height: 1.5em;">// Generated by Unity. Remove this comment to prevent overwriting when exporting again.
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation project(':unityLibrary')
implementation 'com.huawei.hms:push:4.0.3.301'
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
}</p>
3. Configure the mainTemplate.gradle file.
Code:
<p style="line-height: 1.5em;">apply plugin: 'com.android.library'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
**DEPS**}</p>
4. Add the <service> block to the AndroidManifest.xml file.
Code:
<p style="line-height: 1.5em;"><?xml version="1.0" encoding="utf-8"?>
<!-- Generated by Unity. Remove this comment to prevent overwriting when exporting again. -->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service
android:name="com.unity.hms.push.MyPushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest></p>
5. Change the package name.
Go to Edit > Project Settings > Player, click the Android icon, and go to Other Settings in Unity. Then, set Package Name.
The default package name is com.${Company Name}.${Product Name}. You need to change the package name, and the app will be released to AppGallery with the new name.
1.3.4 Adding the agconnect-services.json File​1. Create an app by following instructions in Creating an AppGallery Connect Project and Adding an App to the Project.
Run keytool -list -v -keystore C:\TestApp.keyStore to generate the SHA-256 certificate fingerprint based on the keystore file of the app. Then, configure the fingerprint in AppGallery Connect.
2. Download the agconnect-services.json file and place it in the Assets/Plugins/Android directory of your Unity project.
1.3.5 Enabling Push Kit​For details, please refer to the development guide.
1.4 App Development with the Official Asset​1.4.1 Sample Code​Read in forum
1.4.2 Testing the APK​1. Generate the APK.
Go to File > Build Settings > Android, click Switch Platform and then Build And Run.
2. Create messages to be pushed in AppGallery Connect.
Sign in to AppGallery Connect and click My projects. Go to Push Kit and click Add notification.
3. Run the APK to obtain the device token and configure it in AppGallery Connect.
4. Check logs for data messages.
For more details, you can go to:
l Our official website
l Our Development Documentation page, to find the documents you need
l Reddit to join our developer discussion
l GitHub to download demos and sample codes
l Stack Overflow to solve any integration problems
Read in forum

Beginner: Read the latest news! Integration of Huawei Push Kit in Application 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 explain how to subscribe the topic using integrating the Huawei Push Kit into React Native project. React Native is a tool announced and open-sourced by Facebook in April 2015. It is a cross-platform mobile app development framework that is derived from Facebook's earlier open-source JavaScript framework React. It helps to adapt to native mobile app platforms.
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 and React Native Project Preparation
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, as follows.
8. Click Manage APIs tab and enable Push Kit.
9. Environment set up, refer below link
https://reactnative.dev/docs/environment-setup
10. Create project using below command
Code:
react-native init projectname
11. You can install react native command line interface on npm, using the install -g react-native-cli command as shown below.
Code:
npm install –g react-native-cli
12. Integrate the Hmspush plugin
npm i @hmscore/react-native-hms-push
13. Add the AGC Plugin dependency.
Code:
apply plugin: 'com.huawei.agconnect'
14. Add to dependencies.
Code:
Implementation project (': react-native-hms-push)
15. Project level build.gradle/buildScript directory set minSdkVersion to 19 or higher higher. In android/signingConfigs/config node, enter the keystore file credentials.
Code:
signingConfigs {
config {
storeFile file(<keystore_file_name>)
storePassword ‘<store_password>’
keyAlias ‘<alias>’
keyPassword ‘<key_password>’
}
}
16. Navigate to App level android/build.gradle directory in your React Native project. Follow the steps:
-Add to buildscript/repositories
Code:
maven {url 'http://developer.huawei.com/repo/'}
-Add to buildscript/dependencies
Code:
classpath 'com.huawei.agconnect:agcp:1.3.1.300’'3)
17. Navigate to android/settings.gradle and add the following.
Code:
include ':react-native-hms-push'
project(':react-native-hms-push').projectDir = new File(rootProject.projectDir, '../node_modules/@hmscore/react-native-hms-push/android')
18. Add below permissions in Android.manifest file.
XML:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Development
Obtain a Token:
In order to push notifications to a device, you need to obtain a token. To get the token, use getToken method from HmsPushInstanceId module.
JavaScript:
HmsPushInstanceId.getToken("")
.then((result) => {
this.log("getToken", result);
})
.catch((err) => {
alert("[getToken] Error/Exception: " + JSON.stringify(err));
});
Add Push Notifications
To push a notification message to your device, navigate to your Huawei developer console and choose Project>Growing>Push Kit>Add notification.
Subscribing the Topics
Subscribing to a topic enables the users to receive data messages by the subscribed topic. To subscribe a topic, use subscribe method from HmsPushMessaging module.
JavaScript:
subscribe() {
HmsPushMessaging.subscribe(this.state.topic)
.then((result) => {
this.log("subscribe", result);
})
.catch((err) => {
alert("[subscribe] Error/Exception: " + JSON.stringify(err));
});
}
To push a notification or data message by topic, navigate to the Huawei push console to push a new notification, choose push scope as Subscriber and then select the topic of the message to be sent.
Unsubscribe the topics
Unsubscribing from a topic lets the user not to listen data messages by the unsubscribed topic. To unsubscribe from a topic, use unsubscribe method of the HmsPushMessaging module.
JavaScript:
unsubscribe() {
HmsPushMessaging.unsubscribe(this.state.topic)
.then((result) => {
this.log("unsubscribe", result);
})
.catch((err) => {
alert("[unsubscribe] Error/Exception: " + JSON.stringify(err));
});
}
Run the application (Generating the Signed Apk):
1. Open project directory path in command prompt. Navigate to android directory and run the below command for signing the apk.
Code:
gradlew assembleRelease
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.
Code:
gradlew clean
Conclusion
In this article, we can learn about how to integrate Push Kit and how to subscribe the topic in React native project. 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

Categories

Resources