激励广告
激励广告是一种全屏广告格式,用户观看广告可获得奖金或其他奖励(例如游戏币、重试次数、高级功能使用权等)。
备注
用户可决定是否观看广告或跳过广告,只有观看完广告才能获得奖励。
|
实体 |
描述 |
|
|
如果 |
|
|
用于:
|
激励广告创建示例
import React, { useState } from 'react';
import { Alert, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { AdRequestConfiguration, RewardedAd, RewardedAdLoader } from 'yandex-mobile-ads';
const REWARDED_AD_UNIT_ID = 'demo-rewarded-yandex'; // 替换为您的 ID
const REWARD = {
amount: 50,
type: 'coins',
}
export default function RewardedView() {
const [isLoaded, setIsLoaded] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [rewardedAd, setRewardedAd] = useState<RewardedAd | null>(null);
const loadAd = async () => {
try {
setIsLoading(true);
// 1. 创建加载器
const loader = await RewardedAdLoader.create();
// 2. 创建请求配置
const adRequestConfiguration = new AdRequestConfiguration({
adUnitId: REWARDED_AD_UNIT_ID,
});
// 3. 加载广告
const ad = await loader.loadAd(adRequestConfiguration);
console.log('Rewarded ad loaded');
// 4. 配置事件处理程序
ad.onAdShown = () => {
console.log('Rewarded ad shown');
};
ad.onAdDismissed = () => {
console.log('Rewarded ad dismissed');
setIsLoaded(false);
setRewardedAd(null);
};
ad.onAdClicked = () => {
console.log('Rewarded ad clicked');
};
ad.onAdImpression = (impressionData) => {
console.log('Rewarded impression:', impressionData);
};
ad.onAdFailedToShow = (error) => {
console.error('Rewarded failed to show:', error);
Alert.alert('Ad Failed to Show', JSON.stringify(error));
setIsLoaded(false);
};
ad.onRewarded = () => {
Alert.alert('🎉 Reward!', `You earned ${REWARD.amount} ${REWARD.type}`);
console.log('User rewarded:', REWARD);
};
setRewardedAd(ad);
setIsLoaded(true);
setIsLoading(false);
} catch (error) {
console.error('Failed to load rewarded ad:', error);
Alert.alert('Ad Failed to Load', JSON.stringify(error));
setIsLoading(false);
setIsLoaded(false);
}
};
const showAd = () => {
if (rewardedAd && isLoaded) {
rewardedAd.show();
} else {
Alert.alert('Ad not ready', 'Please load the ad first');
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>Rewarded Ad</Text>
<TouchableOpacity
style={[styles.button, isLoading && styles.buttonDisabled]}
onPress={loadAd}
disabled={isLoading}
>
<Text style={styles.buttonText}>
{isLoading ? 'Loading...' : 'Load Ad'}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.button, styles.showButton, !isLoaded && styles.buttonDisabled]}
onPress={showAd}
disabled={!isLoaded}
>
<Text style={styles.buttonText}>Show Ad & Get Reward</Text>
</TouchableOpacity>
<Text style={styles.status}>
{isLoaded ? '✅ Ad Ready' : '❌ Ad Not Loaded'}
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 40,
},
button: {
backgroundColor: '#007AFF',
paddingVertical: 15,
paddingHorizontal: 40,
borderRadius: 10,
marginVertical: 10,
width: '80%',
alignItems: 'center',
},
showButton: {
backgroundColor: '#34C759',
},
buttonDisabled: {
backgroundColor: '#ccc',
},
buttonText: {
color: '#fff',
fontSize: 18,
fontWeight: '600',
},
status: {
marginTop: 30,
fontSize: 16,
color: '#666',
},
});
检查集成
您可以使用我们的演示项目测试广告格式的效果。