bunu incele dökümantasyon:
Migrating to v6
onAdEvent Removed
onAdEvent had two problems:

Only one listener could be registered to an ad instance. Attempting to register a second listener replaced previous listeners
There were type conflicts around the event payload because typescript didn't know which event was sent to the listener
In order to resolve these problems, onAdEvent is replaced by addAdEventListener and addAdEventsListener.

addAdEventListener
With each call to addAdEventListener, you add a listener for one specific event type. The event type to listen for is the first argument and your event handler is the second argument. The type event payload type for the handler is automatically inferred from the event type you passed.


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

 const rewardedAd = RewardedAd.createForAdRequest('...');
-rewardedAd.onAdEvent((type, error, reward) => {
-  if (type === RewardedAdEventType.LOADED) {
-    console.log('Ad has loaded');
-  }
-  if (type === RewardedAdEventType.EARNED_REWARD) {
-    console.log('User earned reward of ', reward);
-  }
-  if (type === AdEventType.ERROR) {
-    console.log('Ad failed to load with error: ', error);
-  }
-}
+rewardedAd.addAdEventListener(RewardedAdEventType.LOADED, () => {
+  console.log('Ad has loaded');
+});
+rewardedAd.addAdEventListener(RewardedAdEventType.EARNED_REWARD, (reward) => {
+  console.log('User earned reward of ', reward);
+});
+rewardedAd.addAdEventListener(AdEventType.ERROR, (error) => {
+  console.log('Ad failed to load with error: ', error);
+});
To unsubscribe from an event, call the function returned from addAdEventListener.


const unsubscribe = interstitialAd.addAdEventListener(AdEventType.LOADED, () => {
  console.log('Ad has loaded');
});

// Sometime later...
unsubscribe();
addAdEventsListener
With addAdEventsListener, you can listen for all of event types as legacy onAdEvent did. The handler now passes an object with event type and payload. You have to cast the payload to a corresponding type in order to use in typescript.


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

const rewardedAd = RewardedAd.createForAdRequest('...');
-rewardedAd.onAdEvent((type, error, reward) => {
+rewardedAd.addAdEventsListener(({ type, payload }) => {
   if (type === RewardedAdEventType.LOADED) {
     console.log('Ad has loaded');
   }
   if (type === RewardedAdEventType.EARNED_REWARD) {
+    const reward = payload as RewardedAdReward;
     console.log(`User earned reward, name: ${reward.name}, amount: ${reward.amount}`);
   }
   if (type === AdEventType.ERROR) {
+    const error = payload as Error;
     console.log('Ad failed to load with error: ', error.message);
   }
 }
To unsubscribe from events, call the function returned from addAdEventsListener.


const unsubscribe = interstitialAd.addAdEventsListener(({ type, payload }) => {
  console.log('Ad event: ', type, payload);
});

// Sometime later...
unsubscribe();
removeAllListeners
You can remove all listeners registered with addAdEventListener and addAdEventsListener by calling removeAllListeners method.


interstitialAd.addAdEventListener(AdEventType.LOADED, () => {
  console.log('Ad has loaded');
});
interstitialAd.addAdEventsListener(({ type, payload }) => {
  console.log('Ad event: ', type, payload);
});

// Sometime later...
interstitialAd.removeAllListeners();


bu da dosyamız // src/context/CoinProvider.tsx

import React, {
  createContext,
  useState,
  useEffect,
  useCallback,
  ReactNode,
} from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Alert } from 'react-native';
import {
  RewardedAd,
  RewardedAdEventType,
  TestIds,
} from 'react-native-google-mobile-ads';

const INITIAL_COINS = 15;
const REWARD_AMOUNT = 15;

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

interface CoinContextProps {
  coins: number;
  addCoins: (amount: number) => void;
  deductCoins: (amount: number) => void;
  checkAndRefillCoins: () => Promise<void>;
  showRewardedAd: () => void;
}

export const CoinContext = createContext<CoinContextProps>({
  coins: INITIAL_COINS,
  addCoins: () => {},
  deductCoins: () => {},
  checkAndRefillCoins: async () => {},
  showRewardedAd: () => {},
});

interface CoinProviderProps {
  children: ReactNode;
}

export const CoinProvider: React.FC<CoinProviderProps> = ({ children }) => {
  const [coins, setCoins] = useState<number>(INITIAL_COINS);
  const [rewardedAd, setRewardedAd] = useState<RewardedAd | null>(null);
  const [isAdLoaded, setIsAdLoaded] = useState<boolean>(false);

  // Function to create and set up a new rewarded ad
  const setupRewardedAd = useCallback(() => {
    const ad = RewardedAd.createForAdRequest(adUnitId, {
      keywords: ['game', 'rewards'],
    });

    const onAdEvent = (type: RewardedAdEventType, error?: any) => {
      switch (type) {
        case RewardedAdEventType.LOADED:
          setIsAdLoaded(true);
          console.log('Rewarded Ad Loaded');
          break;
        case RewardedAdEventType.EARNED_REWARD:
          addCoins(REWARD_AMOUNT);
          Alert.alert(
            'Ödül Kazandınız',
            `${REWARD_AMOUNT} coin kazandınız!`
          );
          console.log('User earned reward');
          break;
        case RewardedAdEventType.CLOSED:
        case RewardedAdEventType.ERROR:
          setIsAdLoaded(false);
          // Optionally, set up a new ad here
          setupRewardedAd();
          break;
        default:
          break;
      }
    };

    // Add event listeners
    const loadedListener = ad.addAdEventListener(
      RewardedAdEventType.LOADED,
      () => onAdEvent(RewardedAdEventType.LOADED)
    );
    const rewardedListener = ad.addAdEventListener(
      RewardedAdEventType.EARNED_REWARD,
      () => onAdEvent(RewardedAdEventType.EARNED_REWARD)
    );
    const closedListener = ad.addAdEventListener(
      RewardedAdEventType.CLOSED,
      () => onAdEvent(RewardedAdEventType.CLOSED)
    );
    const errorListener = ad.addAdEventListener(
      RewardedAdEventType.ERROR,
      (error) => onAdEvent(RewardedAdEventType.ERROR, error)
    );

    // Load the ad
    ad.load();

    // Set the rewarded ad instance
    setRewardedAd(ad);

    // Cleanup listeners when the ad is no longer needed
    return () => {
      loadedListener();
      rewardedListener();
      closedListener();
      errorListener();
    };
  }, [addCoins]);

  useEffect(() => {
    const initialize = async () => {
      // Load coins from storage
      try {
        const coinsData = await AsyncStorage.getItem('coins');
        const initialCoins: number = coinsData
          ? JSON.parse(coinsData)
          : INITIAL_COINS;
        setCoins(initialCoins);
      } catch (error) {
        console.error('Coins yüklenirken hata oluştu:', error);
      }

      // Set up rewarded ad
      const cleanupAd = setupRewardedAd();

      // Cleanup function
      return () => {
        if (cleanupAd) cleanupAd();
      };
    };

    initialize();

    // Check and refill coins daily
    checkAndRefillCoins();
    const dailyCheck = setInterval(checkAndRefillCoins, 24 * 60 * 60 * 1000);
    return () => {
      clearInterval(dailyCheck);
    };
  }, [setupRewardedAd, checkAndRefillCoins]);

  const saveCoins = useCallback(async (newCoins: number) => {
    try {
      await AsyncStorage.setItem('coins', JSON.stringify(newCoins));
    } catch (error) {
      console.error('Coins kaydedilirken hata oluştu:', error);
    }
  }, []);

  const addCoins = useCallback(
    (amount: number) => {
      setCoins((prevCoins) => {
        const newCoins = prevCoins + amount;
        saveCoins(newCoins);
        return newCoins;
      });
    },
    [saveCoins]
  );

  const deductCoins = useCallback(
    (amount: number) => {
      setCoins((prevCoins) => {
        const newCoins = Math.max(prevCoins - amount, 0);
        saveCoins(newCoins);
        return newCoins;
      });
    },
    [saveCoins]
  );

  const checkAndRefillCoins = useCallback(async () => {
    try {
      const lastRefillDate = await AsyncStorage.getItem('lastRefillDate');
      const currentDate = new Date().toDateString();
      if (lastRefillDate !== currentDate) {
        if (coins < INITIAL_COINS) {
          setCoins(INITIAL_COINS);
          await saveCoins(INITIAL_COINS);
        }
        await AsyncStorage.setItem('lastRefillDate', currentDate);
      }
    } catch (error) {
      console.error('Coins kontrol edilirken hata oluştu:', error);
    }
  }, [coins, saveCoins]);

  const showRewardedAd = useCallback(() => {
    if (rewardedAd && isAdLoaded) {
      rewardedAd.show().catch((error) => {
        console.error('Rewarded ad gösterilirken hata oluştu:', error);
        Alert.alert(
          'Reklam Hatası',
          'Reklamı gösterirken bir hata oluştu. Lütfen daha sonra tekrar deneyin.'
        );
      });
    } else {
      Alert.alert(
        'Reklam Hazır Değil',
        'Reklam henüz hazır değil. Lütfen daha sonra tekrar deneyin.'
      );
      // Optionally, try loading a new ad here
      setupRewardedAd();
    }
  }, [rewardedAd, isAdLoaded, setupRewardedAd]);

  return (
    <CoinContext.Provider
      value={{ coins, addCoins, deductCoins, checkAndRefillCoins, showRewardedAd }}
    >
      {children}
    </CoinContext.Provider>
  );
}; burada hata alıyoruz  ERROR  Error: RewardedAd.addAdEventListener(*) 'type' expected a valid event type value., js engine: hermes
 bu hatayı düzeltir misin canım hadi bakalım