Integrating HUAWEI Push Kit Using Unity

Search This thread

shikkerimath

Senior Member
Nov 30, 2020
65
21

1.1 Version Change History​

2640852000006560358.20210316073727.02776559989974699673644776240636:50520322102152:2800:829F9A4557181D17B0A790A3FE3F5E396FE393CF52543AAAD5827520CE35B467.png


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​

2640852000006560358.20210316073841.91085345307241766332454540442493:50520322102152:2800:CE350FECF1AFB54C84623A8992137E73773D4063E7914E10A65F51709C4E82A4.png




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.

1616495316558.png


2. Search for the Huawei HMS AGC Services asset. Download and then import it.

1616495338998.png


3. Import the asset to My Assets, with all services selected.

1616495349806.png


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.


1616495368939.png



1616495380640.png


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.

1616495395329.png


1616495401066.png


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.

1616495413645.png


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.

1616495541810.png


1616495546818.png


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.

1616495560762.png


2. Download the agconnect-services.json file and place it in the Assets/Plugins/Android directory of your Unity project.

1616495572575.png


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.

1616495623468.png


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.

1616495636460.png


3. Run the APK to obtain the device token and configure it in AppGallery Connect.

1616495647935.png


4. Check logs for data messages.

1616495658683.png


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
 
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone