# Rewarded Ads

Rewarded ads are an engaging ad format that shows a short video ad to the user and in exchange the user will earn a reward. The user must consent and watch the video completely through to the end in order to earn the reward.

{% hint style="info" %}
The speed and stability of users internet connections may vary. It is highly recommended to fetch as far in advance of showing an ad as possible. This helps to ensure that all necessary assets are downloaded. For example, you may want to fetch an ad when a level starts, or after a previous ad has been shown.
{% endhint %}

### Making the Request

The following example shows how to make a request to display the Rewarded ads.

{% tabs %}
{% tab title="Android" %}
Before you can make a request to display a Rewarded ad, you must import the Rewarded class.

{% code title="Kotlin" %}

```kotlin
import com.fyber.fairbid.ads.Rewarded
```

{% endcode %}

{% code title="Java" %}

```java
import com.fyber.fairbid.ads.Rewarded;
```

{% endcode %}

After importing the Rewarded class, you must make a request to display the Rewarded ads.

{% code title="Kotlin" %}

```kotlin
import com.fyber.fairbid.ads.Rewarded

val placementId = "12345"
Rewarded.request(placementId)
```

{% endcode %}

{% code title="Java" %}

```java
import com.fyber.fairbid.ads.Rewarded;

String placementId = "12345";
Rewarded.request(placementId);
```

{% endcode %}

{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
FYBRewarded.delegate = MyRewardedDelegate()
let placementId = "1234"
FYBRewarded.request(placementId)/code
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
FYBRewarded.delegate = MyRewardedDelegate()
let placementId = "1234"
FYBRewarded.request(placementId)
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
string placementId = "1234";

Rewarded.Request(placementId);
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Adding Callbacks

The callback code below is required for SDK to track the activity of your ad properly.

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
Rewarded.setRewardedListener(object : RewardedListener {
    override fun onShow(placementId: String, impressionData: ImpressionData) {
        // Called when the rewarded ad from placement 'placementId' shows up.
        // In case the ad is a video, audio play will start here.
    }

    override fun onClick(placementId: String) {
        // Called when the rewarded ad from placement 'placementId' is clicked
    }

    override fun onHide(placementId: String) {
        // Called when the rewarded ad from placement 'placementId' hides.
        // In case the ad is a video, audio play will stop here.
    }

    override fun onShowFailure(placementId: String, impressionData: ImpressionData) {
        // Called when an error arises when showing the rewarded ad from placement 'placementId'
    }

    override fun onAvailable(placementId: String) {
        // Called when a rewarded ad from placement 'placementId' becomes available
    }

    override fun onUnavailable(placementId: String) {
        // Called when a rewarded ad from placement 'placementId' becomes unavailable
    }

    override fun onCompletion(placementId: String, userRewarded: Boolean) {
        // Called when a rewarded ad from placement 'placementId' finishes playing
    }

    override fun onRequestStart(placementId: String, requestId: String) {
        // Called when a rewarded ad from placement 'placementId' is going to be requested
        // 'requestId' identifies the request across the whole request/show flow
    }
})
```

{% endcode %}

{% code title="Java" %}

```java
Rewarded.setRewardedListener(new RewardedListener() {
    @Override
    public void onShow(String placementId, ImpressionData impressionData) {
        // Called when the rewarded ad from placement 'placementId' shows up. In case the ad is a video, audio play will start here.
    }

    @Override
    public void onClick(String placementId) {
        // Called when the rewarded ad from placement 'placementId' is clicked
    }

    @Override
    public void onHide(String placementId) {
        // Called when the rewarded ad from placement 'placementId' hides. In case the ad is a video, audio play will stop here.
    }

    @Override
    public void onShowFailure(String placementId, ImpressionData impressionData) {
        // Called when an error arises when showing the rewarded ad from placement 'placementId'
    }

    @Override
    public void onAvailable(String placementId) {
        // Called when a rewarded ad from placement 'placementId' becomes available
    }

    @Override
    public void onUnavailable(String placementId) {
        // Called when a rewarded ad from placement 'placementId' becomes unavailable
    }

    @Override
    public void onCompletion(String placementId, boolean userRewarded) {
        // Called when a rewarded ad from placement 'placementId' finishes playing
    }

    @Override
    public void onRequestStart(String placementId, String requestId) {
        // Called when a rewarded ad from placement 'placementId' is going to be requested
        // 'requestId' identifies the request across the whole request/show flow
    }
});
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
class MyRewardedDelegate: NSObject, FYBRewardedDelegate {

    func rewardedIsAvailable(_ placementName: String) {}

    func rewardedIsUnavailable(_ placementName: String) {}

    func rewardedDidShow(_ placementName: String, impressionData: FYBImpressionData) {}

    func rewardedDidFail(toShow placementName: String, withError error: Error, impressionData: FYBImpressionData) {}

    func rewardedDidClick(_ placementName: String) {}

    func rewardedDidComplete(_ placementName: String, userRewarded: Bool) {}

    func rewardedDidDismiss(_ placementName: String) {}

    func rewardedWillRequest(_ placementId: String, withRequestId requestId: String) {}

}
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
#import <FairBidSDK/FairBid.h>

@interface MyRewardedDelegate : NSObject

@end

@implementation MyRewardedDelegate

- (void)rewardedIsAvailable:(NSString *)placementId {
//    Called when a rewarded ad from placement becomes available
}

- (void)rewardedIsUnavailable:(NSString *)placementId {
//    Called when a rewarded ad from placement becomes unavailable
}

- (void)rewardedDidShow:(NSString *)placementId impressionData:(FYBImpressionData *)impressionData {
//    Called when a rewarded ad from placement shows up. Audio play will start here.
}

- (void)rewardedDidFailToShow:(NSString *)placementId withError:(NSError *)error impressionData:(FYBImpressionData *)impressionData {
//    Called when an error arises when showing a rewarded ad from placement
}

- (void)rewardedDidClick:(NSString *)placementId {
//    Called when a rewarded ad from placement is clicked
}

- (void)rewardedDidDismiss:(NSString *)placementId {
//    Called when a rewarded ad from placement hides. Audio play will stop here.
}

- (void)rewardedDidComplete:(NSString *)placementId userRewarded:(BOOL)userRewarded {
//    Called when a rewarded ad finishes playing
}

- (void)rewardedWillRequest:(NSString *)placementId withRequestId:(NSString *)requestId {
//    Called when a rewarded ad is going to be requested.
}

@end
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
public class MyRewardedListener : RewardedListener
{
    public void OnShow(string placementId, ImpressionData impressionData)
    {
        // Called when a rewarded ad from placementId shows up. In case the ad is a video, audio play will start here.
        // On Android, this callback might be called only once the ad is closed.
    }

    public void OnClick(string placementId)
    {
        // Called when a rewarded ad from placement 'placementId' is clicked
    }

    public void OnHide(string placementId)
    {
        // Called when a rewarded ad from placement 'placementId' hides.
    }

    public void OnShowFailure(string placementId, ImpressionData impressionData)
    {
        // Called when an error arises when showing a rewarded ad from placement 'placementId'
    }

    public void OnAvailable(string placementId)
    {
        // Called when a rewarded ad from placement 'placementId' becomes available
    }

    public void OnUnavailable(string placementId)
    {
        // Called when a rewarded ad from placement 'placementId' becomes unavailable
    }

    public void OnCompletion(string placementId, bool userRewarded)
    {
        // Called when a rewarded ad from placement 'placementId' finishes playing.
        // In case the ad is a video, audio play will stop here.
    }

    public void OnRequestStart(string placementId)
    {
        // Called when a rewarded ad from placement 'placementId' is going to be requested
    }
}

Rewarded.SetRewardedListener(new MyRewardedListener());
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Example showing an ad

The following example checks to see if a placement is available and, if it is, uses the show request to display the ad.

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
val placementId = "12345"
if (Rewarded.isAvailable(placementId)) {
    Rewarded.show(placementId, context)
}
```

{% endcode %}

{% code title="Java" %}

```java
val placementId = "12345"
if (Rewarded.isAvailable(placementId)) {
    Rewarded.show(placementId, context)
}
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
let placementId = "1234"
if (FYBRewarded.isAvailable(placementId)) {
    FYBRewarded.show(placementId)
}
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
NSString *placementId = @"1234";
if ([FYBRewarded isAvailable:placementId]) {
    [FYBRewarded show:placementId];
}
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% hint style="warning" %}
If your game needs to be paused before showing an ad (e.g., stop audio playback), this is the best moment to do so. The time between calling this API and the ad actually being shown should be negligible. This approach is preferable to relying on the 'OnShow' callback since, we cannot guarantee it will be called on Android before the ad starts playing.
{% endhint %}

{% code title="C#" %}

```csharp
string placementId = "1234";

if (Interstitial.IsAvailable(placementId)) {
	Interstitial.Show(placementId);
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Server-Side Rewarding

{% tabs %}
{% tab title="Android" %}
Refer to the provided documentation for detailed instructions on configuring [Server-Side Rewarding](https://docs.digitalturbine.com/dt-fairbid/advanced-configurations/server-side-rewarding).
{% endtab %}

{% tab title="iOS" %}
Refer to the provided documentation for detailed instructions on configuring [Server-Side Rewarding](https://docs.digitalturbine.com/dt-fairbid/advanced-configurations/server-side-rewarding).
{% endtab %}

{% tab title="Unity" %}
{% hint style="warning" %}
When using Unity, due to technical limitations on the framework side, we notice that for some very low percentage of users, we receive the OnShowFailure followed by the OnCompletion(true). We're currently working on fixing the issue. However, in the meantime, if you're using client-side rewarding and you consider the OnShowFailure as a callback to trigger a new request and unblock your game's logic, we recommend you delay this check with the following workaround:
{% endhint %}

{% code title="C#" %}

```csharp
using System.Collections;

public class RewardedScene : MonoBehaviour, RewardedListener
{
    private bool CompletionEventReceived;

    private void ShowVideo()
    {
        // Reset the completion event flag
        this.CompletionEventReceived = false;

        // Show rewarded video with a specific placement ID
        Rewarded.Show("your_placement_id");
    }

    private void OnShowFailure()
    {
        // Start a coroutine to wait for user completion
        this.StartCoroutine(this.WaitUserCompletion());
    }

    private void OnCompletion(bool userRewarded)
    {
        // Mark the completion event as received
        this.CompletionEventReceived = true;

        if (userRewarded)
        {
            // Credit the user
        }
        else
        {
            // Handle the case where the user was not rewarded
            this.UserNotRewarded();
        }
    }

    private IEnumerator WaitUserCompletion()
    {
        // Delay the check for 1 second, giving enough time to receive the OnCompletion callback
        yield return new WaitForSeconds(1);

        // If the completion event was not received, handle it
        if (!this.CompletionEventReceived)
        {
            this.UserNotRewarded();
        }
    }

    private void UserNotRewarded()
    {
        // Handle your logic for users who didn't complete the rewarded ad
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}
