![Rewards Ads in Flutter using Admob with Server Side Verification (SSV)](https://www.prepnew.com/wp-content/uploads/2024/03/Rewards-Ads-in-Flutter-using-Admob-with-Server-Side-Verification-SSV.png)
Rewarded ads offer a valuable way to engage users and monetize your Flutter app. However, relying solely on the ad network’s reporting can leave you vulnerable to fraudulent activity. Thankfully, server-side verification (SSV) provides a robust solution to ensure users truly complete the ad before granting rewards. This blog post dives into implementing SSV for rewarded ads in Flutter apps, boosting your app’s security and ad revenue.
Understanding SSV:
SSV shifts the verification process from the client-side app to your server. The ad network sends a signed message containing details about the ad completion to your server. Your server then verifies the signature and data using the ad network’s public key, confirming the ad completion legitimacy before rewarding the user.
Implementation Steps:
1. Configure Ad Unit:
2. Server-Side Setup:
3. Flutter App Integration:
4. Testing and Deployment:
Additional Tips:
Benefits of SSV:
Implementing server-side verification (SSV) for rewarded ads in Flutter apps using Dart
I. Project Setup:
dependencies:
flutter:
sdk: flutter
google_mobile_ads: ^1.7.0+4 # AdMob plugin
http: ^0.13.5 # For making HTTP requests to your server
II. AdMob Configuration:
III. Flutter App Implementation:
1. Initialize AdMob:
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize(); // Initialize AdMob SDK
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// ... (rest of your app's code)
}
2. Create RewardedAd:
RewardedAd _rewardedAd;
// Load and show the rewarded ad when needed
void _loadRewardedAd() {
_rewardedAd = RewardedAd(
adUnitId: your_ad_unit_id, // Insert your actual ad unit ID
listener: _onRewardedAdListener,
);
_rewardedAd.load();
}
void _showRewardedAd() {
if (_rewardedAd.isLoaded) {
_rewardedAd.show();
} else {
print('The rewarded ad hasn\'t loaded yet.');
}
}
void _onRewardedAdListener(RewardedAdEvent event) {
if (event == RewardedAdEvent.userEarnedReward) {
_verifyRewardOnServer(event.reward); // Send reward details to server for verification
}
}
3. Implement Reward Verification:
Future<bool> _verifyRewardOnServer(RewardItem reward) async {
try {
final response = await http.post(
Uri.parse('your_server_endpoint_url'), // Replace with your actual endpoint
body: {
'userId': reward.userId,
'type': reward.type,
'amount': reward.amount,
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return data['isValidReward'];
} else {
print('Error verifying reward on server: ${response.statusCode}');
return false;
}
} catch (error) {
print('Error sending reward verification request: $error');
return false;
}
}
IV. Server-Side Verification (Implementation varies depending on your server-side technology and preferences):
Here is an example using Node.js
Prerequisites 📋
If you are using firebase cloud functions, you can safely choose to have nodejs 12 runtime. Add a function to listent to incoming http requests.
Installing 🔧
Install via NPM
$ npm install --save admob-rewarded-ads-ssv
const admobSSV = require('admob-rewarded-ads-ssv');
//Add callback to your rewarded ads in your admob account.
//Make sure you listen to 'get' request.
app.get('/ssv-verify', (req, res, next) => {
// If you want to debug then send second param as true
// admobSSV.verify(req.url, true);
admobSSV.verify(req.url)
.then(() => {
//Verification Successful
})
.catch((e) => {
//Verification Failed
//console.error(e.message);
});
});