How to Quickly Integrate Cloud Functions of AppGallery Connect Into Cocos-Based App
In this tutorial, learn how you can quickly integrate the cloud functions of AppGallery Connect into your Cocos-based app.
Join the DZone community and get the full member experience.Join For Free
If you want to quickly experience this service, see the demo on GitHub.
- SDK Version: agconnect-storage:18.104.22.1680
- Platform: Cocos Creator
- Device: HUAWEI P40 Pro
- AppGallery Connect: https://developer.huawei.com/consumer/en/service/josp/agc/index.html
2. Enabling and Configuring Cloud Functions in AppGallery Connect
Note: Currently, Cloud Functions is still under beta. To use the service, you need to send an email for application. For details, check here.
1. Create an app first and add it to a project, or select an app from the project list on the My projects page in AppGallery Connect.
Under the project, go to Build > Cloud Functions, and click Enable now.
2. Create a function based on the description in the following codelab: https://developer.huawei.com/consumer/en/codelab/CloudFunctions/index.html#4
3. Create an HTTP trigger and save the suffix of the value of Trigger URL in the Details area as the trigger identifier of future client requests.
3. Integrating the Cloud Functions SDK in Cocos Creator
1. Integrate the SDK
Official documentation: https://docs.cocos.com/creator/manual/en/cocos-service/agc-applinking.html
1. On the Service panel of Cocos Creator, find Cloud Functions. Currently, the Cloud Functions SDK supports only the Android platform.
2. Before integrating a service, you need to associate the service with an app. Click Association. In the dialog box that is displayed, click Create. The Cocos console is displayed.
3. On the Cocos console, create a game.
4. Go back to Cocos Creator and create, refresh, or select an association.
5. Return to the Cloud Functions page, and enable the service.
2. Download the JSON File
1. Go to Project settings in AppGallery Connect and download the latest agconnect-services.json file.
2. Save the downloaded agconnect-services.json file to the settings directory in your Cocos project.
4. Configuring the UI Layout
Configure buttons for triggering a cloud function, obtaining the timeout interval, and setting the timeout interval separately.
5. Cloud Functions Development
1. Initialize Cloud Functions in the
2. Call Cloud Functions with input parameters.
3. Call Cloud Functions without input parameters.
4. Obtain and set the timeout interval of a function.
6. Packaging and Testing
In Cocos Creator, go to Project > Build…, package an Android app, and install it to your device. To verify the functions, click the buttons from top to bottom.
Cloud Functions allows you to keep all functions and related code on the cloud side. You only need to call the
wrap.call method to trigger the functions, which reduces the code amount and package size of your APK package.
For more details, please check:
- Cloud Functions development guide: https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-introduction
- Cocos documentation: https://docs.cocos.com/creator/manual/en/cocos-service/agc-cloudfunc.html
- Cloud Function Demo: https://github.com/AppGalleryConnect/agc-demos/tree/main/Android/agc-cloudfunction-demo-java
Opinions expressed by DZone contributors are their own.