[Huawei Developers] muraliameakula:
Introduction

In this article, we will be integrating Huawei Game Services kit in flutter application. You will have access to range of development capabilities. You can promote your game quickly and more efficiently to Huawei’s vast users as Huawei Game Services allows users to login game using Huawei IDs. You can also use the service to quickly implement achievements, game events, and game addiction prevention functions and perform in-depth game operations based on user and content localization.

Huawei Game Services Capabilities

Game Login

Achievements

Floating window*

Game Addiction prevention*

Events

Leaderboards

Save Games*

Player statistics*

Access to Basic Game Information*

Note: Restricted to regions (*)

Development Overview

You need to install Flutter and Dart plugin in IDE and I assume that you have prior knowledge about the Flutter and Dart.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone with API 4.x.x or above (with the USB cable),Which is used for debugging.

Software Requirements

Java JDK 1.7 or later.

Android studio software or Visual Studio or Code installed.

HMS Core (APK) 4.X or later.

Integration process

Step 1. Create flutter project.

Step 2. Add the App level gradle dependencies, choose inside project Android > app > build.gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

Add root level gradle dependencies.

maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.4.2.301'

Step 3: Add the below permissions in Android Manifest file.

<uses-permission android:name="android.permission.INTERNET" />

Step 4: Add plugin path in pubspec.yaml file under dependencies.

Step 5: Create a project in AppGallery Connect, find here.

pubspec.yaml

name: gameservice234demo

description: A new Flutter project.

# The following line prevents the package from being accidentally published to

# pub.dev using `pub publish`. This is preferred for private packages.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html

version: 1.0.0+1

environment:

sdk: ">=2.12.0 <3.0.0"

dependencies:

flutter:

sdk: flutter

huawei_account:

path: ../huawei_account

huawei_gameservice:

path: ../huawei_gameservice

# The following adds the Cupertino Icons font to your application.

# Use with the CupertinoIcons class for iOS style icons.

cupertino_icons: ^1.0.2

dev_dependencies:

flutter_test:

sdk: flutter

# For information on the generic Dart part of this file, see the

# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.

flutter:

# The following line ensures that the Material Icons font is

# included with your application, so that you can use the icons in

# the material Icons class.

uses-material-design: true

How do I launch or initialize the game?

void init() async {
await JosAppsClient.init();
}

Use Huawei ID for login

Future<void> login() async {
helper = new HmsAuthParamHelper()
..setIdToken()
..setAccessToken()
..setAuthorizationCode()
..setEmail()
..setProfile();
huaweiId = await HmsAuthService.signIn(authParamHelper: helper);
if (huaweiId != null) {
setState(() {
isLoggedIn = true;
msg = huaweiId!.displayName;
loginLabel = "Logged in as";
print(msg);
});
getPlayer();
} else {
setState(() {
msg = " Inside else ";
});
}
}

How do I get Achievements list?

Future<void> getAchievements() async {
try {
List<Achievement> result =
await AchievementClient.getAchievementList(true);
print("Achievement:" + result.toString());
} on PlatformException catch (e) {
print("Error on getAchievementList API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
}

How do I displaying the Achievements List Page of HUAWEI AppAssistant using Intent?

void showAchievementsIntent() {
try {
AchievementClient.showAchievementListIntent();
} on PlatformException catch (e) {
print("Error on showAchievementListIntent API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
}

How do I call Floating window?

try {
await BuoyClient.showFloatWindow();
} on PlatformException catch (e) {
print("Error on showFloatWindow API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}

How do I get All Events?

Future<void> getEvents() async {
try {
List<GameEvent> result = await EventsClient.getEventList(true);
print("Events: " + result.toString());
} on PlatformException catch (e) {
print(
"Error on getEventList API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
}

How do I submit an Event?

Future<void> sendEvent(String eventId) async {
try {
await EventsClient.grow(eventId, 1);
print("************** Event sent **************");
} on PlatformException catch (e) {
print(
"Error on grow API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
}

How do I get All Leaderboard data?

Future<void> getLeaderboardList() async {
// check the leaderboard status
int result = await RankingClient.getRankingSwitchStatus();
// set leaderboard status
int result2 = await RankingClient.setRankingSwitchStatus(1);
List<Ranking> rankings = await RankingClient.getAllRankingSummaries(true);
print(rankings);
}

How do I submit the ranking score?

try {
int score = 102;
RankingClient.submitRankingScores(rankingId, score);
} on PlatformException catch (e) {
print("Error on submitRankingScores API, Error: ${e.code}, Error Description:${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
Or
try {
int score = 125;
ScoreSubmissionInfo result = await RankingClient.submitScoreWithResult(rankingId, score);
} on PlatformException catch (e) {
print("Error on submitScoreWithResult API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}

How do I displaying the Leaderboard List Page of HUAWEI AppAssistant using Intent?

void showLeaderboardIntent() {
try {
RankingClient.showTotalRankingsIntent();
} on PlatformException catch (e) {
print("Error on showLeaderboardListIntent API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");
}
}

Result

Tricks and Tips

Make sure that you have downloaded latest plugin.

Make sure that updated plugin path in yaml.

Make sure that plugin unzipped in parent directory of project.

Makes sure that agconnect-services.json file added.

Make sure dependencies are added in build file.

Run flutter pug get after adding dependencies.

Generating SHA-256 certificate fingerprint in android studio and configure in Ag-connect.

Conclusion

In this article, we have learnt how to integrate capabilities of Huawei Game Services kit in flutter application. Yu can promote your game quickly and more efficiently to Huawei’s vast users as Huawei Game Services allows users to login game using Huawei IDs and achieve by implementing its capabilities in your application. Similar way you can use Huawei Game Services as per user requirement in your application.

Thank you so much for reading, I hope this article helps you to understand the Huawei Game Services capabilities in flutter.

Reference

GameServices Kit

Plutter Plugin

Original Source