DEV Community

Cover image for Integration of Push kit in Unity based game
Basavaraj Navi
Basavaraj Navi

Posted on

Integration of Push kit in Unity based game

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 or directly from the AppGallery Push Kit 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
  2. Navigate to Project settings and download the configuration file. Image description
  3. Enable Push Kit from Manage APIs section. Image description
  4. Click Agree the Push service Agreement.
  5. Select Data storage location.
  6. Click Enable now Push notification. Image description
  7. Send Notification from the console. Image description
  8. Enter all the required details and click on Submit. Image description

Game Development

  1. Create a new game in Unity. Image description
  2. Now add game components and let us start game development.
  3. Download HMS Unity Plugin from below site. Image description
  4. Open Unity Engine and import the downloaded HMS Plugin. Choose Assets > Import Package> Custom Package Image description
  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));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Result

Image description
Image description

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

Top comments (0)