在React Native中多次显示AdMob奖励广告

时间:2020-04-22 14:33:53

标签: android react-native admob admob-rewardedvideoad

我创建了一个按钮,用于在点击广告时显示奖励广告。 现在有两个问题: 1.加载广告花费了太多时间(在发生任何事情之前,我可以单击按钮一次或两次)。 2.我想在广告关闭后立即重新加载。它可以工作,但应用程序需要重新启动。

AdMobRewardedComponent.js

  async componentDidMount() {
  await setTestDeviceIDAsync("EMULATOR");
  AdMobRewarded.setAdUnitID("ca-app-pub-3940256099942544/5224354917");

  AdMobRewarded.addEventListener("rewardedVideoDidLoad", () => {
  console.log("VideoLoaded")
  });
  AdMobRewarded.addEventListener("rewardedVideoDidFailToLoad", () =>
  console.log("FailedToLoad")
  );
  AdMobRewarded.addEventListener("rewardedVideoDidOpen", () =>
  console.log("Opened")
  );
  AdMobRewarded.addEventListener("rewardedVideoDidClose", () => {
    loadAd(request.build());
  console.log("Closed")
  });
  AdMobRewarded.addEventListener("rewardedVideoWillLeaveApplication", () =>
  console.log("LeaveApp")
  );
  AdMobRewarded.addEventListener("rewardedVideoDidStart", () =>
  console.log("Started")
  );
  AdMobRewarded.addEventListener("rewardedVideoDidRewardUser", () =>
  console.log("Rewarded"),
  );
  await AdMobRewarded.requestAdAsync();
}

componentWillUnmount() {
  AdMobRewarded.removeAllListeners();
}

_handlePress = async () => {
  await AdMobRewarded.showAdAsync();
};

render() {
  const { loadedAd } = this.state;
  return (
  <TouchableButton onPress={this._handlePress} title="Coins erhalten!" image="adButton" status="active" style={styles.adButton}/>
    );
  }
};

是否可以在不重新启动整个应用的情况下请求新广告? 谢谢您的回答!

1 个答案:

答案 0 :(得分:0)

为了防止多次按下按钮的问题,可以使用防抖动功能: React Native: Using lodash debounce

或者,您可以在商店中管理打开的广告,这样可以确保您没有两次打开广告,并且可以在上一个广告关闭后再打开一个新广告,例如:

X = [linspace(-8*pi,8*pi,7e6), linspace(-0.1,0.1,6e6)];
timeit(@()method1(X))
timeit(@()method2(X))

function Y = method1(X)
n = size(X, 1);
m = size(X, 2);
Y = zeros(n, m);
d = n*m; 

for i = 1:d
    x = X(i);
    if abs(x)<0.1
        Y(i) = -1/6+x.^2/120-x.^4/5040+x.^6/362880;
    else
        Y(i) = (sin(x)-x).*(x.^(-3));
    end
end
end

function Y = method2(X)
Y = (sin(X)-X).*(X.^(-3));
i = abs(X) < 0.1;
Y(i) = -1/6+X(i).^2/120-X(i).^4/5040+X(i).^6/362880;
end

function Y = method3(X)
Y = (sin(X)./X - 1) ./ (X.*X);
i = abs(X) < 0.1;
Y(i) = horner(X(i));
end

function y = horner (x)
pow = x.*x;
y = -1/6+pow.*(1/120+pow.*(-1/5040+pow./362880));
end