[Huawei Developers] Freemind R:
Introduction:

This article will demonstrate to Integrate Push kit in Cocos using Huawei HMS Core App Services Plugin. Huawei Push Kit is a messaging service. It establishes a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on users devices in real time. This helps to maintain closer ties with users and increases user awareness and engagement with your apps.

Note: Cocos is free but not open source.

Requirements:

1. Cocos Creator tool

2. Notepad++ for editing and writing script

3. HMS Device

4. Android Studio for Analyzing APK and Checking Log

Output:

Push Notification, Data Message.

Steps to Integrate:

1. Install and launch cocos creator.

2. Make sure to configure NDK Root and Android SDK Root in Cocos Creator, choose File> Settings>Select Native Develop.

3. Click in Cocos Dashboard, click New.

Create project name in Name section and click on hello-world and then Create and open.

Click on Project tab and then Build.

Enter Platform, Build Path and Package Name in build window.

Note: The package name should be same in app gallery connect.

In the same build popup, in the keystore section we can use default keystore (default location is /Users/<username>/.android/debug.keystore) and we can generate SHA key or we can create custom keystore and can generate SHA key.In Command line terminal execute the command below and get SHA key:

keytool -list -v -keystore keystorepath\keystorename.keystore

Steps to create custom key store file:

a) Uncheck use debug keystore

b) In keystore path section click on New button.

c) Enter all the details in New Keystore details and then click Save.

4. Step 1: Create a project in AppGallery using created SHA key

Step 2: Configure the signing certificate fingerprint

Choose Project settings > General information. In the App information area, click the icon next to SHA 256 certificate fingerprint, and enter the obtained SHA-256 certificate fingerprint.

Step 3: Download agconnect-services.json file and put in setting folder in cocos project.

Example:

Right click on Assets folder and then select Open in Explorer

Place agconnect-services.json file inside settings folder

5. Click Enable for Cocos SDKHub in Service tab.

Click on Association button and register project in Cocos Console

Associating a Game in Cocos Creator You can enable third-party services for your app in Cocos Creator. But first, you need to set a Cocos AppID. Select a created game, and click Association, as shown in the following figures.

Select Plugin Manage under Service tab.

Select Push option in Plugin Manage under Service tab.

Place mouse cursor over the "Configured" text so that "Edit" and "Delete" icons will be appeared. Click on edit icon.

Put public key(from AppGallery connect) in the "Payment Public key" field and select "en" option for "Support language" field and then click OK.

Script code for Notification and Data message:

onLoad: function () {
sdkhub.getPushPlugin().startPush();
sdkhub.getPushPlugin().setListener(this.onPushResult, this);
},

onPushResult: function (code, msg) {
console.log("on push result action.");
console.log("code: " + code);
console.log("massage: " + msg);
this.label.string=msg;
this.showToast(code, msg);
switch (code) {
case sdkhub.PushResultCode.kStartPushSucceed:
console.log("kStartPushSucceed", msg);
break;
}
},

6. Click on Build, Compile, Play under the Project tab to generate APK file.

Tips & Tricks:

Make sure to keep the same package name as kept in the Huawei AppGallery Connect project.

Conclusion:

To promote products and launch marketing activities, games, e-commerce apps, and utility apps can call Push Kit APIs to select target audiences by tag and push messages to them, attracting more users and improving the marketing activity conversion rate.

References:

> Note: this version of the documentation has been archived and is no longer maintained. Please move to the latest version. · Cocos Creator

docs.cocos.com