[Huawei Developers] muraliameakula:
Introduction

In this article, we will be integrating Huawei Ads and Game Services kit in flutter application. You can 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 with 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 Ads kit helps developer to monetize application.

Huawei supports following Ads types

Banner

Interstitial

Native

Reward

Splash

Instream(Roll)

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
huawei_ads:
path: ../huawei_ads_301

# 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 Ads SDK?

HwAds.init();

How do I load Splash Ads?

void showSplashAd() {
SplashAd _splashAd = createSplashAd();
_splashAd
..loadAd(
adSlotId: "testq6zq98hecj",
orientation: SplashAdOrientation.portrait,
adParam: AdParam(),
topMargin: 20);
Future.delayed(Duration(seconds: 7), () {
_splashAd.destroy();
});
}
static SplashAd createSplashAd() {
SplashAd _splashAd = new SplashAd(
adType: SplashAdType.above,
ownerText: ' Huawei SplashAd',
footerText: 'Test SplashAd',
); // Splash Ad
return _splashAd;
}

How do I load Native Ad?

static NativeAd createNativeAd() {
NativeStyles stylesSmall = NativeStyles();
stylesSmall.setCallToAction(fontSize: 8);
stylesSmall.setFlag(fontSize: 10);
stylesSmall.setSource(fontSize: 11);
NativeAdConfiguration configuration = NativeAdConfiguration();
configuration.choicesPosition = NativeAdChoicesPosition.topLeft;
return NativeAd(
// Your ad slot id
adSlotId: "testu7m3hc4gvm",
controller: NativeAdController(
adConfiguration: configuration,
listener: (AdEvent event, {int? errorCode}) {
print("Native Ad event : $event");
}),
type: NativeAdType.small,
styles: stylesSmall,
);
}

How do I load Interstitial Ad?

void showInterstitialAd() {
InterstitialAd interstitialAd =
InterstitialAd(adSlotId: "teste9ih9j0rc3", adParam: AdParam());
interstitialAd.setAdListener = (AdEvent event, {int? errorCode}) {
print("InterstitialAd event : $event");
};
interstitialAd.loadAd();
interstitialAd.show();
}

How do I load Rewarded Ad?

static Future<void> showRewardAd() async {
RewardAd rewardAd = RewardAd();
await rewardAd.loadAd(
adSlotId: "testx9dtjwj8hp",
adParam: AdParam(),
);
rewardAd.show();
rewardAd.setRewardAdListener =
(RewardAdEvent event, {Reward? reward, int? errorCode}) {
print("RewardAd event : $event");
if (event == RewardAdEvent.rewarded) {
print('Received reward : ${reward!.toJson().toString()}');
}
};
}

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);
//To show RankingIntent
RankingClient.showTotalRankingsIntent();
}

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

Game Services previous article you can check out here

Conclusion

In this article, we have learnt how to integrate capabilities of Huawei Ads with Game Services kit in flutter application. You can promote your game quickly and more efficiently to Huawei’s vast users as Huawei Game Services allows users to login with Huawei IDs and this can be achieved by implementing its capabilities in your application. Developer can easily integrate and monetize the application which helps developer to grow financial long with application. Similar way you can use Huawei Ads with 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 Ads with Game Services capabilities in flutter.

Reference

GameServices Kit

Plutter Plugin Game services

Ads Kit

Original Source