💡Tips
- Before calling any ad API, make sure
McSdk.initialize()has completed initialization; otherwise the call will be ignored- It is recommended to set up the Listener and load ads as early as possible at app launch to ensure ads are ready when you need to show them
- The SDK has built-in exponential backoff retry logic on load failure to avoid frequent requests that may reduce fill rate
const String _adUnitId = "your mediation unit id";
int _retryAttempt = 0;
void initInterstitialAd() {
McSdk.setInterstitialListener(McInterstitialListener(
onAdLoadedCallback: (McAd ad) {
// Ad loaded successfully, reset retry count
_retryAttempt = 0;
},
onAdLoadFailedCallback: (String adUnitId, McError error) {
// Ad failed to load, retry with exponential backoff
_retryAttempt++;
if (_retryAttempt >= 3) {
return;
}
final delaySeconds = pow(2, min(3, _retryAttempt)).toInt();
Future.delayed(Duration(seconds: delaySeconds), () {
McSdk.loadInterstitial(_adUnitId);
});
},
onAdDisplayedCallback: (McAd ad) {
// Ad displayed successfully, preload next ad
McSdk.loadInterstitial(_adUnitId);
},
onAdDisplayFailedCallback: (McAd ad, McError error) {
// Ad failed to display, reload
McSdk.loadInterstitial(_adUnitId);
},
onAdClickedCallback: (McAd ad) {
// User clicked the ad
},
onAdHiddenCallback: (McAd ad) {
// Ad closed, preload next ad
McSdk.loadInterstitial(_adUnitId);
},
onAdRevenuePaidCallback: (McAd ad) {
// Ad revenue callback
},
onAdLoadFinishedCallback: (String adUnitId) {
// Current load round finished (fires regardless of success or failure)
},
));
// Initiate load request
McSdk.loadInterstitial(_adUnitId);
}
Future<void> showInterstitialAd() async {
final isReady = await McSdk.isInterstitialReady(_adUnitId);
if (isReady == true) {
McSdk.showInterstitial(_adUnitId);
} else {
McSdk.loadInterstitial(_adUnitId);
}
}
// Set Extra Parameter, only for Max
McSdk.setInterstitialExtraParameter(_adUnitId, "key", "value");
// Set Local Extra Parameter
McSdk.setInterstitialLocalExtraParameter(_adUnitId, "key", value);
// Parameters must be set before loadInterstitial() and apply to all subsequent load requests