# Callbacks The three interfaces you implement. Every method is called on the main thread (in Unity, the game thread), always after the call that caused it has returned. *NGA SDK 0.1.0 preview: you install 0.1.0 from Maven Central and NGA's public repositories. Every 0.x version is a preview under the NGA SDK License, and the API can change until 1.0. Questions: hi@nga.network. This page: https://nga.io/docs/sdk/reference/callbacks/* ## `NGAListener` Passed to `initialize`. One per app. **Unity (C#)** ```csharp public interface NGAListener { void OnInitialized(NGAInitResult result); void OnError(NGAError error); // optional: implement empty void OnAdClosedAfterRestart(string placementId, string opportunityId, bool rewardGranted); // optional: implement empty } ``` **Android (Kotlin)** ```kotlin interface NGAListener { fun onInitialized(result: NGAInitResult) fun onError(error: NGAError) {} // optional fun onAdClosedAfterRestart(placementId: String, opportunityId: String, rewardGranted: Boolean) {} // optional } ``` **Android (Java)** ```java public interface NGAListener { void onInitialized(NGAInitResult result); default void onError(NGAError error) {} // optional default void onAdClosedAfterRestart(String placementId, String opportunityId, boolean rewardGranted) {} // optional } ``` **iOS (Swift)** ```swift @objc protocol NGAListener { func onInitialized(_ result: NGAInitResult) @objc optional func onError(_ error: NGAError) @objc optional func onAdClosedAfterRestart(placementId: String, opportunityId: String, rewardGranted: Bool) } ``` **iOS (Objective-C)** ```objectivec @protocol NGAListener - (void)onInitialized:(NGAInitResult *)result; @optional - (void)onError:(NGAError *)error; - (void)onAdClosedAfterRestartWithPlacementId:(NSString *)placementId opportunityId:(NSString *)opportunityId rewardGranted:(BOOL)rewardGranted; @end ``` | Method | Called | | --- | --- | | `onInitialized` | once per `initialize` call, with an [`NGAInitResult`](https://nga.io/docs/sdk/reference/values/#ngainitresult) | | `onError` | after init, only for `DISABLED / app_key.disabled` or `sdk.retired`, `NOT_INITIALIZED / queue.discarded` and `INTERNAL / event_queue.reset`. Not for retries or network errors | | `onAdClosedAfterRestart` | after a successful `onInitialized`, once for each NGA ad that was on screen when the process died. `rewardGranted` is true if a rewarded ad's reward was earned; pay it unless `onRewardResult` already did. The latest 5 reports that come while no listener is set are kept until an `initialize` sets one | ## `NGAAdLoadCallback` Passed to `loadAd`. **Unity (C#)** ```csharp public interface NGAAdLoadCallback { void OnLoaded(NGAAd ad); void OnFailed(string placementId, NGAError error); void OnExpired(NGAAd ad); // optional: implement empty } ``` **Android (Kotlin)** ```kotlin interface NGAAdLoadCallback { fun onLoaded(ad: NGAAd) fun onFailed(placementId: String, error: NGAError) fun onExpired(ad: NGAAd) {} // optional } ``` **Android (Java)** ```java public interface NGAAdLoadCallback { void onLoaded(NGAAd ad); void onFailed(String placementId, NGAError error); default void onExpired(NGAAd ad) {} // optional } ``` **iOS (Swift)** ```swift @objc protocol NGAAdLoadCallback { func onLoaded(_ ad: NGAAd) func onFailed(placementId: String, error: NGAError) @objc optional func onExpired(_ ad: NGAAd) } ``` **iOS (Objective-C)** ```objectivec @protocol NGAAdLoadCallback - (void)onLoaded:(id)ad; - (void)onFailedWithPlacementId:(NSString *)placementId error:(NGAError *)error; @optional - (void)onExpired:(id)ad; @end ``` | Method | Called | | --- | --- | | `onLoaded` | when the ad is cached, verified and ready to show. Every distinct callback passed to `loadAd` for this ad gets it | | `onFailed` | when the load fails. Every callback waiting on the same load gets the same error. `retryable` and `retryAfterMillis` say whether and when to try again | | `onExpired` | when the ad can no longer be shown: it expired, its files were cleared, child treatment began, or NGA stopped the SDK (the app key switched off, or this SDK version or build retired). Not called for an ad that was shown | ## `NGAAdShowListener` Passed to `show`. One per show. **Unity (C#)** ```csharp public interface NGAAdShowListener { void OnShowStarted(NGAAd ad); void OnClicked(NGAAd ad); // optional: implement empty void OnRewardResult(NGAAd ad, NGARewardResult result); // optional: implement empty void OnCompleted(NGAAd ad, NGAShowCompletion completion); void OnClosed(NGAAd ad); void OnShowFailed(NGAAd ad, NGAError error); } ``` **Android (Kotlin)** ```kotlin interface NGAAdShowListener { fun onShowStarted(ad: NGAAd) fun onClicked(ad: NGAAd) {} // optional fun onRewardResult(ad: NGAAd, result: NGARewardResult) {} // optional fun onCompleted(ad: NGAAd, completion: NGAShowCompletion) fun onClosed(ad: NGAAd) fun onShowFailed(ad: NGAAd, error: NGAError) } ``` **Android (Java)** ```java public interface NGAAdShowListener { void onShowStarted(NGAAd ad); default void onClicked(NGAAd ad) {} // optional default void onRewardResult(NGAAd ad, NGARewardResult result) {} // optional void onCompleted(NGAAd ad, NGAShowCompletion completion); void onClosed(NGAAd ad); void onShowFailed(NGAAd ad, NGAError error); } ``` **iOS (Swift)** ```swift @objc protocol NGAAdShowListener { func onShowStarted(_ ad: NGAAd) @objc optional func onClicked(_ ad: NGAAd) @objc optional func onRewardResult(_ ad: NGAAd, result: NGARewardResult) func onCompleted(_ ad: NGAAd, completion: NGAShowCompletion) func onClosed(_ ad: NGAAd) func onShowFailed(_ ad: NGAAd, error: NGAError) } ``` **iOS (Objective-C)** ```objectivec @protocol NGAAdShowListener - (void)onShowStarted:(id)ad; - (void)onCompleted:(id)ad completion:(NGAShowCompletion)completion; - (void)onClosed:(id)ad; - (void)onShowFailed:(id)ad error:(NGAError *)error; @optional - (void)onClicked:(id)ad; - (void)onRewardResult:(id)ad result:(NGARewardResult *)result; @end ``` A show that counts its impression calls these, in this order: | Order | Method | Called | | --- | --- | --- | | 1 | `onShowStarted` | when the impression counted: at least half the ad visible for a second, in the foreground, screen on | | 2 | `onClicked` | zero or more times, once per tap that opens the store | | 3 | `onRewardResult` | rewarded placements only, exactly once, with an [`NGARewardResult`](https://nga.io/docs/sdk/reference/values/#ngarewardresult) | | 4 | `onCompleted` | with `COMPLETED` or `SKIPPED`. A full-screen ad is `COMPLETED` when it was on screen as long as its video; a native banner always is | | 5 | `onClosed` | always last. The ad is used up (`CONSUMED`) | | — | `onShowFailed` | when the show could not start, or it ended in a failure: its renderer crashed and the retry failed, or the SDK's own drawing or screen code failed (`RENDER_FAILED`); nothing follows it | A show that ends before its impression calls `onClosed` alone: nothing is paid and nothing earned. **Every show ends exactly once,** with one `onClosed` or one `onShowFailed`, and nothing follows it. **After `onShowFailed`, the ad's state says what to do.** Check `isReady()`. - `ALREADY_SHOWING` leaves the ad as it was: `READY` when another ad is on screen (`show.already_showing.fullscreen`, `show.already_showing.banner`), `SHOWING` when it is this ad (`show.already_showing.same_handle`). - `ACTIVITY_NOT_RESUMED`, `INVALID_SHOW_TARGET` and `RENDER_NOT_AVAILABLE / webview.unavailable` leave it `READY`: show it again when the cause is gone. - `EXPIRED`, `AD_CONSUMED`, `NOT_READY` and `RENDER_FAILED` mean it is spent: load again.