[Huawei Developers] ask011:
INTRODUCTION

In this article we will learn how to integrate the Push Kit into a Unity project from start to finish, constantly in groups and video game development communities questions and difficulties are triggered for developers so within this article I would like to touch on different problems that can be easily solved just by following a few steps and modifying certain values within the Unity editor.

MOST COMMON QUESTIONS IN DEV COMMUNITIES

Through the development communities, we have been presented with different questions that I have been collecting little by little, below I share some that are commonly repeated.

After importing the Evil Mind plugin, my project won't run on my device?

Add the Push Manager prefab and when I run the game in game mode it doesn't compile

How can I get the token that Push Kit returns to me when it is executed?

Unity cannot locate the Android SDK and I cannot run the project.

ARTICLE GOAL

Our goal in this article will be to be able to send a notification to our phone from App Gallery Connect using the token that HMS Push Kit gives us, we will achieve this by solving the problems that arise at the time of configuration.

STEPS

Configuration App Gallery Connect.

Configure Unity to run our project.

Import Evil Mind Plugin.

Configure our project with App Gallery Connect.

Create the class and the code to get the answer.

Obtain the log of our device

Add the token to App Gallery Connect

Try our game

APP GALLERY CONFIGURATION

As we know we must create a project in App Gallery Connect and add an app in order to configure the necessary data for our project. Let's start by logging into the AGC console to create a project.

Within the projects section we can create a new project where we must create an App which will be added to our project.

Once the application is created we must fill in the following data in the form, for this it will be necessary to create our project in Unity and assign the package name.

Let's go to unity to create the necessary data, Open unity and create a new project. Once the project is created, select Build Settings> Player Settings to assign your game data.

Remember to confirm that in Other Settings you also have your package correctly. Here we will answer one of the questions we have at the beginning of this article.

After importing the Evil Mind plugin, my project won't run on my device? This generally happens to us because we do not have the Backend value configured correctly and the minimum supported API, in the following image I share the data that you should place.

With this configuration we will be able to run our game on the device that we have connected, remember that if you are using a GMS device it will be necessary to install the HMS Core apk which you can download from this league. https://m.apkpure.com/es/hms-core/com.huawei.hwid/download once downloaded you can run huawei mobile services.

Running your project on the device should have a screen similar to this, we have not added the button yet but we will do it later, the important thing is that your scene compiles correctly.

UNITY CONFIGURATION

Let's start now with the configuration of our project in Unity. What we must do is import the Evil Mind plugin to be able to obtain the upper menu of Huawei and to be able to place the data obtained from AGC. Download the plugin from the repository that I share with you now. https://github.com/EvilMindDevs/hms-unity-plugin

Download the pcustom package to your computer and import it into Unity.

You must import it from the import tab and select custom package, once imported an option will be enabled in the upper menu with the title of Huawei, when you click on it you will get the following pop up.

Download the json file from AGC and replace it in Unity the path to find the json you can find in the following image

Open the agconnect file and start putting the necessary data to paste into the Huawei plugin. In the image I am showing you in yellow what data you must take to place them in the plugin window.

When you finish assigning the data, click on the Configure Manifest button. IMPORTANT JUST GIVE A CLICK, OTHERWISE YOU WILL ADD LINES TO YOUR MANIFEST AND THE ERROR WILL BE PRESENTED Merge Manifest.

Check your android Manifest take into account that when you import the plugin, a default Manifest will be downloaded in your project, that Manifest Default has packages that belong to the example, so it is important that you only have the following <providers> as I show in the image

Add the Push Manager prefab to your scene, you can also add a button inside a Canvas to initialize the Push Kit service, leave it in the onStart () method;

Run your project again to identify errors if they occur during the compilation and construction of the project. This will not generate any effect in Push Kit nor will it grant you the token because we have not configured the SHA256 signature, possibly the console will indicate that the fingerprint has not been configured. We are going to configure this signature. Create your keystore in your project folder, go to the folder where your project is with your console and execute the following command.

Replace the name of your keystore

keytool -list -v -keystore d:key.keystore

Get the Sha 256 and place it in AGC.

CODING

We will create a script that is responsible for initializing the service and granting us the token of our device and then sending the notification through token.

namespace HmsPlugin
{
public class PushKitManager : MonoBehaviour, IPushListener
{

public Action<string> OnTokenSuccess { get; set; }
public Action<Exception> OnTokenFailure { get; set; }

public Action<RemoteMessage> OnMessageReceivedSuccess { get; set; }

// Start is called before the first frame update
void Start()
{
PushManager.Listener = this;
var token = PushManager.Token;
Debug.Log($"[HMS] Push token from GetToken is {token}");
if (token != null)
{
OnTokenSuccess?.Invoke(token);
}
}

public void OnNewToken(string token)
{
Debug.Log($"[HMS] Push token from OnNewToken is {token}");
if (token != null)
{
OnTokenSuccess?.Invoke(token);
}
}

public void OnTokenError(Exception e)
{
Debug.Log("Error asking for Push token");
Debug.Log(e.StackTrace);
OnTokenFailure?.Invoke(e);
}

public void OnMessageReceived(RemoteMessage remoteMessage)
{
OnMessageReceivedSuccess?.Invoke(remoteMessage);
}
}
}

We will also create a method only as an example to be able to initialize the service and obtain the token

public void StartNotificationService()
{
PushManager.Listener = this;
var token = PushManager.Token;
Debug.Log($"The token {token}");
if(token != null)
{

}OnTokenSuccess?.Invoke(token);

}

Now we need to obtain the token of our phone and that the Log will return us and we will place it in the push notification service.

Fill in the necessary data to send the notification such as the Title, the Body and the name of the notification. You must also configure the device parameters. In this section is where you must add the token obtained from your device.

Now just launch the notification

TIPS AND TRICKS

I dont know how to obtain logs from my Android Device. In the following steps you'll learn how to debug from your Device.

For troubleshooting and debugging, it is often very helpful to capture the device logs. The device logs contain information about the status of a running application on the device and any errors encountered. The IAP Engineering team requires logs for open issues. It is assumed here that you have an Android device configured with Developer options and attached to a PC with a USB cable. These steps show the process on a Windows system and works similarly on a Mac

1. In Unity, find the location of the Android SDK. From the Edit menu, choose Preferences/External Tools

2. Look for the Android SDK. On my system it was C:\Users\[username]\AppData\Local\Android\sdk

3. Open a command prompt, and change to this directory.

4. Change directory to the platform-tools subdirectory.

5. You should see adb.exe in this directory.

6. With the device connected via USB, run the following command:

adb devices (Mac is ./adb devices)

7. This should return device serial number followed by the word "device"

8. Run "adb logcat | findstr -i unity" (use grep on Mac) in the terminal window. This will find anything with the word "unity" in the output, case insensitive.

9. This will provide the contents of the device log that are associated with a running Unity application

The output can be quite verbose. You can browse through the log however, looking for such words as "error" and "exception". Sometimes customer support will ask for device logs, in which case you can send the output to a text file with the command "adb logcat -d | findstr -i unity > log.txt"

CONCLUSION

Push notifications are one of the trending tools in marketing arenas. No arena has been left untouched by the tool- after engaging users brilliantly on an e-commerce store, it’s the turn of the gaming app push notifications.

Mobile gaming apps have gone too far popular in today’s arena so much so that mobiles have been developed specifically for the game, consisting of the hardware that would meet the demands of the users.In this article we learned how to implement HMS Kit Push notification in oour Unity Game an it was very fast.

REFERENCES

Push Kit - Cloud Message Push- Cross-Platform Messaging Solution - HUAWEI Developer

Push Kit can satisfy the relevance and freshness of content update that your users want. Push instant messages from the cloud to user devices.

developer.huawei.com

Read In Forum

SPANISH VIDEOTUTORIAL

If you speak or understand spanish here you have the videotutorial.