invertase / react-native-google-mobile-ads

React Native Google Mobile Ads enables you to monetize your app with AdMob.
https://docs.page/invertase/react-native-google-mobile-ads
Other
673 stars 135 forks source link

RewardedAd.show() The requested RewardedAd has not loaded and could not be shown. #509

Closed SaGaR1084 closed 6 months ago

SaGaR1084 commented 9 months ago

I am trying to load RewardedAd but it shows me this error : RewardedAd.show() The requested RewardedAd has not loaded and could not be shown

Screenshot 2023-12-29 at 12 04 57 PM

This is My Code :

import React, { useEffect, useState } from 'react';
import { Button, View } from 'react-native';

import {
  RewardedAd,
  RewardedAdEventType,
  TestIds
} from 'react-native-google-mobile-ads';

const adUnitId = **DEV**
  ? TestIds.REWARDED
  : 'ca-app-pub-xxxxxxxxxxxxx/yyyyyyyyyyyyyy';

const rewarded = RewardedAd.createForAdRequest(adUnitId, {
  keywords: ['fashion', 'clothing'],
});

const MyApp = () => {
const [loaded, setLoaded] = useState(false);

useEffect(() => {
  const unsubscribeLoaded = rewarded.addAdEventListener(RewardedAdEventType.LOADED, () => {
    setLoaded(true);
  });
  const unsubscribeEarned = rewarded.addAdEventListener(
    RewardedAdEventType.EARNED_REWARD,
    reward => {
      console.log('User earned reward of ', reward);
    },
  );

    // Start loading the rewarded ad straight away
    rewarded.load();

    // Unsubscribe from events on unmount
    return () => {
      unsubscribeLoaded();
      unsubscribeEarned();
    };

}, []);

  console.log('loaded===', loaded);
  return (
    <View>
      <Button
      title="Show Rewarded Ad"
      onPress={() => {
      rewarded.show();
      }}
      />
    </View>
  );
};

export default MyApp;

Platforms

Only on Android

package.json

"react": "18.2.0",
"react-native": "0.71.8",
"react-native-google-mobile-ads": "^12.6.0",

app.json

{
  "name": "MyApp",
  "displayName": "MyApp",
  "react-native-google-mobile-ads": {
    "android_app_id": "ca-app-pub-xxxxxxxxxxxxx/yyyyyyyyyyyyyy"
  }
}
github-actions[bot] commented 8 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

markwilcox commented 8 months ago

FYI - we see this issue in production. We check the ad is loaded immediately before showing a dialog with the option to watch the ad for the "reward" but some smallish percentage of the time we hit this error anyway.

I know the code above is direct from the documentation but you should definitely check the ad is actually loaded before trying to show it.

github-actions[bot] commented 7 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.