Rewarded ads
Rewarded ads are a full-screen ad format where users get bonuses or other rewards (for example, in-game coins, retries, access to premium features, and so on) for watching ads.
Note
It's the user who decides whether to watch the ad or skip it, receiving a reward only if they watch it to the end.
Note
Examples showing how all the format types work are available in the demo project.
|
Entity |
Description |
|
|
Use:
|
Example of creating a rewarded ad
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:yandex_mobileads/mobile_ads.dart';
class RewardedPage extends StatefulWidget {
const RewardedPage({super.key});
@override
State<RewardedPage> createState() => _RewardedPageState();
}
class _RewardedPageState extends State<RewardedPage> {
static const _tag = 'Rewarded';
static const _adUnitId = 'demo-rewarded-yandex';
RewardedAdLoader? _loader;
RewardedAd? _ad;
String _status = 'Initializing...';
String _reward = '';
@override
void initState() {
super.initState();
_init();
}
@override
void dispose() {
_ad?.destroy();
super.dispose();
}
Future<void> _init() async {
setState(() => _status = 'Creating loader...');
_loader = await RewardedAdLoader.create(
onAdLoaded: (ad) {
debugPrint('[$_tag] onAdLoaded');
if (!mounted) { ad.destroy(); return; }
_ad = ad;
setState(() => _status = 'Showing...');
_show();
},
onAdFailedToLoad: (error) {
debugPrint('[$_tag] onAdFailedToLoad: ${error.description}');
if (mounted) setState(() => _status = 'Error: ${error.description}');
},
);
setState(() => _status = 'Loading ad...');
await _loader!.loadAd(
adRequestConfiguration: AdRequestConfiguration(adUnitId: _adUnitId),
);
}
Future<void> _show() async {
final ad = _ad;
if (ad == null) return;
ad.setAdEventListener(
eventListener: RewardedAdEventListener(
onAdShown: () => debugPrint('[$_tag] onAdShown'),
onAdDismissed: () {
debugPrint('[$_tag] onAdDismissed');
if (mounted) setState(() => _status = _reward.isEmpty ? 'Closed' : 'Reward received!');
},
onAdClicked: () => debugPrint('[$_tag] onAdClicked'),
onAdFailedToShow: (error) {
debugPrint('[$_tag] onAdFailedToShow: ${error.description}');
if (mounted) setState(() => _status = 'Show error: ${error.description}');
},
onAdImpression: (ImpressionData impressionData) =>
debugPrint('[$_tag] onAdImpression: $impressionData'),
onRewarded: (reward) {
debugPrint('[$_tag] onRewarded: ${reward.amount} ${reward.type}');
if (mounted) setState(() => _reward = '+${reward.amount} ${reward.type}');
},
),
);
await ad.show();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Rewarded')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (_reward.isEmpty && !_status.contains('Error') && !_status.contains('Closed'))
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text(_status),
if (_reward.isNotEmpty) ...[
const SizedBox(height: 16),
Text(_reward, style: const TextStyle(color: Colors.green, fontSize: 24, fontWeight: FontWeight.bold)),
],
],
),
),
);
}
}
Checking integration
Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat in Android Studio:
[Integration] Ad type rewarded was integrated successfully