Search code examples
androidxamarinxamarin.androidadmob

RewardedAd.Show() has deprecated overloads


In my Android game (a Xamarin app), I have Google Rewarded Ads implemented in my app as such:

    public class InternalRewardedAdCallback : RewardedAdCallback
    {
        .
        .
        .
    }

    RewardedAd.Show(Platform.CurrentActivity, new InternalRewardedAdCallback());

But this is now deprecated, and the ads wont load. Looking at the documentation, we have to now use a listener to reward the user:

    public class InternalOnUserEarnedRewardListener : OnUserEarnedRewardListener
    {

    }

    RewardedAd.Show(Platform.CurrentActivity, new InternalOnUserEarnedRewardListener());

However this does not work. There only exists an IOnUserEarnedRewardListener and NO OnUserEarnedRewardListener class. Implementing the interface doesn't seem like a proper solution. Has anyone succeeded in updating their ads code?

I am using the latest Xamarin.GooglePlayServices.Ads version 119.7.0

UPDATE

Here is what it looks like when trying to implement it:

private class InternalOnUserEarnedRewardListener : IOnUserEarnedRewardListener
{
    public IntPtr Handle => throw new NotImplementedException();

    public int JniIdentityHashCode => throw new NotImplementedException();

    public JniObjectReference PeerReference => throw new NotImplementedException();

    public JniPeerMembers JniPeerMembers => throw new NotImplementedException();

    public JniManagedPeerStates JniManagedPeerState => throw new NotImplementedException();

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Disposed()
    {
        throw new NotImplementedException();
    }

    public void DisposeUnlessReferenced()
    {
        throw new NotImplementedException();
    }

    public void Finalized()
    {
        throw new NotImplementedException();
    }

    public void OnUserEarnedReward(IRewardItem p0)
    {
        throw new NotImplementedException();
    }

    public void SetJniIdentityHashCode(int value)
    {
        throw new NotImplementedException();
    }

    public void SetJniManagedPeerState(JniManagedPeerStates value)
    {
        throw new NotImplementedException();
    }

    public void SetPeerReference(JniObjectReference reference)
    {
        throw new NotImplementedException();
    }

    public void UnregisterFromRuntime()
    {
        throw new NotImplementedException();
    }
}

Solution

  • You can create a class that inherit Java.Lang.Object and implement the IOnUserEarnedRewardListener interface:

    public class CustomOnUserEarnedRewardListener : Java.Lang.Object, IOnUserEarnedRewardListener
    {
        public event EventHandler OnRewarded;
    
        public void OnUserEarnedReward(IRewardItem rewardItem)
        {
            OnRewarded?.Invoke(this, EventArgs.Empty);
        }
    }
    

    And then show the RewardedAd:

    var rewardListener = new CustomOnUserEarnedRewardListener();
    rewardListener.OnRewarded += (s, e) =>
    {
        //Handle the reward
    }
    
    _rewardedAd.Show(_currentActivity, rewardListener);
    

    Note: Google Mobile Ads SDK 19.7.0 introduces many new APIs, deprecates many classes and APIs and renames many classes in preparation for version 20.0.0. Because of this, Xamarin.GooglePlayServices.Ads version 119.7.0 is not 100% stable.