Я пытаюсь использовать рекламу с useEffect, но реклама не загружается.
Код без useEffect работает, но запускает событие загрузки как минимум 3 раза подряд
export const WatchAdScreen = (props) => {
const [adReady, setAdReady] = useState(false)
const [loading, setLoading ] = useState(true)
const rewarded = RewardedAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['gaming', 'streaming', 'mobile games' ],
})
rewarded.onAdEvent((type, error, reward) => {
console.log({type}, {error}, {reward})
if (type === RewardedAdEventType.LOADED) {
console.log('ad reward loaded')
setAdReady(true)
setLoading(false)
}
if (type === RewardedAdEventType.EARNED_REWARD) {
showToast('Reward confirmed')
console.log('User earned reward of ', reward)
}
if (!rewarded.loaded) {
setAdReady(false)
rewarded.load()
}
if (type === RewardedAdEventType.ERROR) {
console.log('error not def')
}
if (type === 'closed') {
console.log('ad closed')
}
if (error) {
console.log('error', error)
}
})
// Start loading the rewarded ad straight away
rewarded.load()
const showToast = (message) => {
ToastAndroid.show(message, ToastAndroid.SHORT)
}
const BackIcon = (props) => (
<Icon {...props} name='arrow-circle-left-outline'/>
)
return (<Layout> ....)
}
Если я следую документации от rnfirebase, я просто застреваю на экране загрузки, setReady(True)
никогда не вызывается, как и никакие другие события в слушателе.
const rewarded = RewardedAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['gaming', 'streaming', 'mobile games' ],
})
export const WatchAdScreen = (props) => {
const [adReady, setAdReady] = useState(false)
const [loading, setLoading ] = useState(true)
useEffect(() => {
setLoading(false)
console.log('enter reward screen')
const eventListener = rewarded.onAdEvent((type, error, reward) => {
console.log({type}, {error}, {reward})
if (type === RewardedAdEventType.LOADED) {
console.log('ad reward loaded')
setAdReady(true)
}
if (type === RewardedAdEventType.EARNED_REWARD) {
showToast('Reward confirmed')
console.log('User earned reward of ', reward)
}
if (!rewarded.loaded) {
setAdReady(false)
rewarded.load()
}
if (type === RewardedAdEventType.ERROR) {
console.log('error not def')
}
if (type === 'closed') {
console.log('ad closed')
}
if (error) {
console.log('error', error)
}
})
// Start loading the rewarded ad straight away
rewarded.load()
console.log('I got here')
// Unsubscribe from events on unmount
return () => {
console.log('exit reward screen')
eventListener()
}
}, [])
return (Layout)
}
Если я перемещаюсь:
const rewarded = RewardedAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['gaming', 'streaming', 'mobile games' ],
})
внутри WatchAdScreen
, все события запускаются правильно, однако, когда я звоню rewards.show()
, я получаю Error: firebase.admob() RewardedAd.show() The requested RewardedAd has not loaded and could not be shown.