How Can I Quickly Integrate Cloud Functions of AppGallery Connect into My Cocos-based App?

1. Environment

SDK Version:agconnect-storage:1.3.0.300

2. Enabling and Configuring Cloud Functions in AppGallery Connect

Note: Currently, Cloud Functions is still under beta state. To use the service, you need to send an email for application. For details, check:

3. Integrating the Cloud Functions SDK in Cocos Creator

1. Integrate the SDK.

Official documentation:

2. Download the JSON file.

1. Go to Project settings in AppGallery Connect and download the latest agconnect-services.json file.

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 start block.

start () {  
this._func = huawei.agc.func.funcService;
},
callWithParam:function() {
this._func.wrap("testcodelab-$latest").call((err, data) => {
if (err !== null) {
console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
}
console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
}, {
year: "2020",
}
);
},
callWithoutParam:function() {
this._func.wrap("testcodelab-$latest").call((err, data) => {
if (err !== null) {
console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
}
console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
});
},
setTime:function() {
this._func.wrap("testcodelab-$latest").setTimeout(100,3);
console.log("Cloud Function", `setTime: ` + 100);

},

getTime:function() {
let currTime = this._func.wrap("testcodelab-$latest").getTimeout();
console.log("Cloud Function", `currTime: ` + currTime);
},

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.

7. Summary

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store