Currently Cocos Creator already supports HMS Core and HUAWEI AppGallery Connect (hereinafter referred to as AGC). Developers can use HMS Core and AGC to quickly publish games with one click, reducing a series of complex operations, such as SDK access and application uploading during development, helping you create high-quality and innovative applications, and enhance the gaming experience.
This article will take you step by step to send messages through Huawei Push Service in Cocos.
Description of main steps
-
Download and install Cocos Creator, and create a project on the Cocos webpage and Cocos Creator.
-
Create and configure application information on AGC, then configure Cocos SDKHub and build flow, add HUAWEI_HMS (Push) plug-in and HUAWEI AppGallery Connect.
-
Write code to get the token, then package and compile to generate Apk.
- Test the sending of notification bar messages and transparent messages.
Detailed steps
Step 1: Download and install Cocos Creator, and create a project on Cocos webpage and Cocos Creator
-
Access link http://www.cocos2d-x.org/products#creator , download and install Cocos Creator.
-
Log in to the Cocos developer account and create a project (https://passport.cocos.com/auth/signup)。
- Open Cocos Creator and create a project.
During the creation process, if you encounter the following error message, then you need to download the editor first.
Click Editor to download.
Step 2 : Create and configure application information on AGC, then configure Cocos SDKHub and development flow, add HUAWEI_HMS (Push) plug-in and HUAWEI AppGallery Connect.
-
Refer to the following link to create an application and configure application information on AGC: https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/android-config-agc-0000001050170137
-
The main steps include:
- Generate signature fingerprint certificate;
-
-
Configure the signature fingerprint certificate;
-
Set data storage location;
- Open the corresponding service;
Download the configuration file (agconnect-services.json);
-
Follow the path in the figure below to open the service panel and find Cocos SDKHub.
-
Log in to Cocos Creator and select the project you created.
-
Click to enter Cocos SDKHub and click "Plugin Manage".
-
Select Push and click OK.
-
Click the edit icon in the panel (see figure below).
-
Add the json file downloaded from AGC
-
Click on the menu Project, select Build to create
- On the Build panel, select HUAWEI AppGallery Connect, click "build" and "compile" to compile
If there is an error in the figure below during the creation process, you need to install the NDK first.
Step 3 : Write the code to get the token, then package and compile to generate Apk
The following figure shows the structure of the entire project:
- Add the following code to the js-sdkhub.js file:
var onPushResult = function (code, msg) {
console.log("on push result action.");
console.log("code: " + code);
console.log("msg: " + msg);
}
var init = function () {
let _global = global || window;
_global.sdkhub = _global.sdkhub || {};
sdkhub.getPushPlugin = () => null;
if (typeof SDKHub === 'undefined')
return;
sdkhub.getPushPlugin = () => SDKHub.AgentManager.getInstance().getPushPlugin();
sdkhub.getPushPlugin().startPush();
sdkhub.getPushPlugin().setListener(onPushResult, this);
}
init();
- After clicking "Build", perform "Compile" to package the application. At this time, you can install the APK on the test device for testing.
If you do not configure the fingerprint certificate correctly, then you will encounter the following error during the running of the App:
In the log, you can find the device token through the tag "jswrapper|HUB_LOG".
Step 4 : Test the sending of notification message and transparent message
- Refer to the link guide, log in to AGC and select Push Kit https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/msg-sending-0000001050042119
a. Notification bar message test result
b. Test result of transparent message transmission
At this point, you have completed all the configuration and development processes for using Huawei Push Service in Cocos. Now you can officially use Huawei Push Service in your application to push messages.
For more details, please refer to:
Official website of Huawei Developer Alliance:https://developer.huawei.com/consumer/cn/hms
Obtain development guidance documents:https://developer.huawei.com/consumer/cn/doc/development
To participate in developer discussions, please go to the Reddit community:https://www.reddit.com/r/HuaweiDevelopers/
To download the demo and sample code, please go to Github:https://github.com/HMS-Core
To solve integration problems, please go to Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
Original link:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203376511132610301&fid=18
Author: Meng Yang