Integration of Push kit in Unity based game
Tags: android, hms, huawei, unity3d
Published: 2022-02-16 16:29:18

## Introduction
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. These messages can be sent at any time and even if your app is not running at that time. To achieve this you need to follow steps.

**Huawei Push Kit** is a messaging service developed by Huawei for developers to send messages to apps on users’ device in real time. Push Kit supports two types of messages: **notification messages** and **data messages**, which we will cover both in this tutorial. You can send notifications and data messages to your users from your server using the [Push Kit APIs](https://developer.huawei.com/consumer/en/doc/development/HMS-3-References/push-server-send) or directly from the [AppGallery Push Kit Console](https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-console).

**Things required**
1. Unity Engine must be installed in the system.
2. Huawei phone or cloud debugging.
3. Visual Studio 2019
4. Android SDK & NDK

**Steps to integrate**
1. Sign In and Create or Choose a project on AppGallery Connect portal.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g09h6rp6jwyoml35a9pn.png) 
2. Navigate to Project settings and download the configuration file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrhb0jw1k8ak7i1n9zy1.png) 
3. Enable Push Kit from Manage APIs section.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqia7hbfwqpopcgdgasy.png)
4. Click Agree the Push service Agreement.
5. Select Data storage location.
6. Click Enable now Push notification.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3233sg09zv8ij2af0nsa.png) 
7. Send Notification from the console.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prh2t0tkjypnzum6q5wi.png) 
8. Enter all the required details and click on Submit.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqntiz1s7fra54du6gk5.png)
 
**Game Development**
1. Create a new game in Unity.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/253qyx9budk25b32k6p6.png)  
2. Now add game components and let us start game development.
3. Download HMS Unity Plugin from below site.
[](https://github.com/EvilMindDevs/hms-unity-plugin/releases)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1stc44icyxyarmn3148c.png) 
4. Open Unity Engine and import the downloaded HMS Plugin.
Choose Assets > Import Package> Custom Package
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnapaw06354sz94l94xw.png) 
5. Choose Huawei > App Gallery.
6. Provide the AppId and other details from agconnect-service.json file and click configure Manifest.
7. Create Huawei Push Kit based scripts.

```
using UnityEngine;
namespace HuaweiHms{
    public class IPushServiceListener:AndroidJavaProxy{
        public IPushServiceListener():base("com.unity.hms.push.IPushService"){}
        public virtual void onMessageReceived(RemoteMessage arg0) {   }

        public void onMessageReceived(AndroidJavaObject arg0) {
            onMessageReceived(HmsUtil.GetHmsBase<RemoteMessage>(arg0));
        }
        
        public virtual void onMessageSent(string arg0) {
            
        }
        
        public virtual void onNewToken(string arg0) {
            
        }
        
        public virtual void onSendError(string arg0, BaseException arg1) {
            
        }
        public void onSendError(string arg0, AndroidJavaObject arg1) {
            onSendError(arg0,HmsUtil.GetHmsBase<BaseException>(arg1));
        }
        
        public virtual void onTokenError(BaseException arg0) {
            
        }
        public void onTokenError(AndroidJavaObject arg0) {
            onTokenError(HmsUtil.GetHmsBase<BaseException>(arg0));
        }
    }
}
```

## Result
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4o6gcze3v8l572vmu6c.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ngiqplsz1xckdqwixnr.png)
  
## Tips and Tricks
1. HMS plugin v1.2.0.
2. Make sure that you have installed HMS Core.
3. The Push Kit server allows a maximum of 1,000 tokens for sending messages at a time.
4. If exceed more than 1k it need to be sent batch wise.
## Conclusion
In this article, we have learnt what is push service, how to integrate in Unity based game. And also we have learnt that how to send notification from Huawei console to device.

## References
[Unity Push kit](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060)