[Huawei Developers] muraliameakula:
Introduction

You can use App Messaging of AppGallery Connect to subscribe and send relevant messages to target active users of your app to encourage them to use key app functions. For example, you can send in-app messages to encourage users to subscribe certain products, provide tips on passing a game level or recommend activities of a restaurant.

App Messaging allows you to customize our messages look and the way they will be sent, and define events for triggering message sending to your users at the right moment.

AG Connect supports three types of messages as follows:

1. Pop-up message

2. Image message

3. Banner message

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.

Follows the steps.

1. Create Unity Project.

Open unity Hub.

Click NEW, select 3D, Project Name and Location.

Click CREATE, as follows:

2. Click Asset Store, search Huawei HMS Core App Services and click Import, as follows.

3. Once import is successful, verify directory in Assets> Huawei HMS Core App Services path, as follows.

4. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.

5. Generate a SHA-256 certificate fingerprint.

To generating SHA-256 certificate fingerprint use below command.

keytool -list -v -keystore D:\Unity\projects_unity\file_name.keystore -alias alias_name

6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.

7. Choose Project Settings > Player and update package name.

8. Open LauncherTemplate.gradle and add below line.

apply plugin: 'com.huawei.agconnect'

implementation 'com.huawei.hms:hianalytics:5.1.0.301'
implementation 'com.huawei.agconnect:agconnect-core:1.4.2.301'

9. Open AndroidManifest file and add below permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

10. Open "baseProjectTemplate.gradle" and add lines, as follows.

classpath'com.huawei.agconnect:agcp:1.4.1.300'

maven {url 'https://developer.huawei.com/repo/'}

11. Open "mainTemplate.gradle" and add lines, as follows.

implementation 'com.huawei.agconnect:agconnect-core:1.4.2.301'
implementation 'com.huawei.hms:hianalytics:5.1.0.301'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.huawei.agconnect:agconnect-appmessaging:1.4.1.300'

12. Enable debug mode use in cmd prompt [optional].

adb shell setprop debug.huawei.hms.analytics.app package_name

13. Configuring project in AGC

14. Create Scripts folder and create a class.

APPMessaging.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiService;
using HuaweiService.appmessage;
using HuaweiService.analytic;
using HuaweiService.push;
using Exception = HuaweiService.Exception;
public class APPMessaging : MonoBehaviour
{
private HiAnalyticsInstance instance;
private AGConnectAppMessaging appMessaging;
private void Start()
{
instance = HiAnalytics.getInstance(new Context());
appMessaging = AGConnectAppMessaging.getInstance();
appMessaging.setFetchMessageEnable(true);
appMessaging.setDisplayEnable(true);
instance.setAnalyticsEnabled(true);
getAAID();
}
private void getAAID(){
// Task result = instance.getAAID();
Task id = HmsInstanceId.getInstance(new Context()).getAAID();
id.addOnSuccessListener(new HmsSuccessListener<AAIDResult>((aaidResult) =>
{
string aaId = aaidResult.getId();
Debug.Log("AAID==>> "+aaId);
})).addOnFailureListener(new HmsFailureListener((e) =>
{
Debug.Log("AAID==>> Failed");
}));
}
public delegate void SuccessCallBack<T>(T o);
public class HmsSuccessListener<T>:OnSuccessListener{
public SuccessCallBack<T> CallBack;
public HmsSuccessListener(SuccessCallBack<T> c){
CallBack = c;
}
public void onSuccess(T arg0)
{
if(CallBack != null)
{
CallBack.Invoke(arg0);
}
}
public override void onSuccess(AndroidJavaObject arg0){
if(CallBack !=null)
{
Type type = typeof(T);
IHmsBase ret = (IHmsBase)Activator.CreateInstance(type);
ret.obj = arg0;
CallBack.Invoke((T)ret);
}
}
}
public delegate void SuccessCallBack(AndroidJavaObject object);
public delegate void FailureCallBack(Exception e);
public class HmsFailureListener:OnFailureListener{
public FailureCallBack CallBack;
public HmsFailureListener(FailureCallBack c){
CallBack = c;
}
public override void onFailure(Exception arg0){
if(CallBack !=null){
CallBack.Invoke(arg0);
}
}
}
}

AndroidManifest.xml

<?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">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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>
</application>
</manifest>

15. Follow the steps, as shown in image:

a. Create empty GameObject.

b. Assign script APPMessaging(as per you script name) to empty GameObject.

16. To build apk and run in device, choose File > Build Settings > Build for apk or Build and Run for run on connected device.

Result

Sign in to AppGallery Connect and click My projects.

Find and click your project.

Navigate to Grow > App Messaging and click New.

Select Name and Description.

Click Test in Operation drop-down.

Enter text in Test user AAID and click Save.

Tips and Tricks

Always use the latest version of the library.

Add agconnect-services.json file without fail.

Add SHA-256 fingerprint without fail.

Make sure dependencies added in build files.

Make sure you have enable debug mode.

Make sure the image url is correct and aspect ratio for Portrait is 3:2 (300x200) and Landscape aspect ratio is 1:1 or 3:2 (100 x100 or 300x200).

Conclusion

In this article, we have learnt to integrate Huawei AppMessaging service into unity game development. AppMessaging provides services sending promotional messages and new updates and also we can target users to promote some products.

Thanks for reading the article, please do likes, comments and your queries or suggestions.

References

App Messaging - click here

Unity App Messaging Integration Manual - click here

Original source