Integration practice | teach you how to access Huawei push service through Cocos

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

  1. Download and install Cocos Creator, and create a project on the Cocos webpage and Cocos Creator.

  2. Create and configure application information on AGC, then configure Cocos SDKHub and build flow, add HUAWEI_HMS (Push) plug-in and HUAWEI AppGallery Connect.

  3. Write code to get the token, then package and compile to generate Apk.

  4. 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

  1. Access link http://www.cocos2d-x.org/products#creator , download and install Cocos Creator.Insert picture description here

  2. Log in to the Cocos developer account and create a project (https://passport.cocos.com/auth/signup)。Insert picture description here

  3. Open Cocos Creator and create a project.
    Insert picture description here

During the creation process, if you encounter the following error message, then you need to download the editor first.
Insert picture description here

Click Editor to download.
Insert picture description here

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.

  1. 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);
Insert picture description here

  1. Follow the path in the figure below to open the service panel and find Cocos SDKHub.
    Insert picture description here

  2. Log in to Cocos Creator and select the project you created.
    Insert picture description here
    Insert picture description here

  3. Click to enter Cocos SDKHub and click "Plugin Manage".
    Insert picture description here

  4. Select Push and click OK.
    Insert picture description here
    Insert picture description here

  5. Click the edit icon in the panel (see figure below).
    Insert picture description here

  6. Add the json file downloaded from AGC
    Insert picture description here

  7. Click on the menu Project, select Build to create
    Insert picture description here
    Insert picture description here

  8. On the Build panel, select HUAWEI AppGallery Connect, click "build" and "compile" to compile
    Insert picture description here

If there is an error in the figure below during the creation process, you need to install the NDK first.
Insert picture description here
Insert picture description here
Insert picture description here

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:
Insert picture description here
Insert picture description here

  1. 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();
  1. After clicking "Build", perform "Compile" to package the application. At this time, you can install the APK on the test device for testing.
    Insert picture description here

If you do not configure the fingerprint certificate correctly, then you will encounter the following error during the running of the App:
Insert picture description here

In the log, you can find the device token through the tag "jswrapper|HUB_LOG".
Insert picture description here

Step 4 : Test the sending of notification message and transparent message

  1. 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
Insert picture description here
Insert picture description here

b. Test result of transparent message transmission
Insert picture description here
Insert picture description here

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

Guess you like

Origin blog.51cto.com/14772288/2541422