Skip to content

UnityAds adapter: context handling (GH-3077) + null/empty token to failure#30

Merged
vishaldhiman-unity merged 6 commits into
mainfrom
fix/unity-adapter-token-and-context
Jul 17, 2026
Merged

UnityAds adapter: context handling (GH-3077) + null/empty token to failure#30
vishaldhiman-unity merged 6 commits into
mainfrom
fix/unity-adapter-token-and-context

Conversation

@vishaldhiman-unity

@vishaldhiman-unity vishaldhiman-unity commented Jul 9, 2026

Copy link
Copy Markdown

Separates two Unity Ads adapter fixes from the delayed v4.19.0 fork-sync/new-API migration PR, so they can ship independently off main.

  • Context handling (GH-3077): pass a nullable Activity to the Unity SDK on show instead of hard-rejecting non-Activity contexts; drop unused ERROR_MSG_CONTEXT_NULL.
  • Null/empty bidding token to failure: collectSignals treated a null/empty token as an empty success (billed as a no-fill). Route null/empty tokens to SignalCallbacks.onFailure via RoutingTokenListener (new error code 112).

Tests: ./gradlew :unity:test passes.

vishaldhiman-unity and others added 2 commits July 8, 2026 11:45
Pass a nullable Activity to the Unity SDK instead of hard-rejecting a
non-Activity context on show. Removes the unused ERROR_MSG_CONTEXT_NULL.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
collectSignals treated a null/empty token as an empty success, so the
bidder billed Unity as a no-fill. Route null/empty tokens to
SignalCallbacks.onFailure via RoutingTokenListener (new error code 112).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extracts and ships two Unity Ads adapter fixes independently: (1) relaxes show-time context handling by passing a nullable Activity into the Unity SDK instead of hard-failing non-Activity contexts, and (2) treats null/empty RTB bidding tokens as an error by routing them to SignalCallbacks.onFailure (new adapter error code 112) rather than reporting an “empty success”.

Changes:

  • Update interstitial + rewarded showAd to pass Activity? (null when context is not an Activity) to UnityAds.show(...).
  • Add RoutingTokenListener to route null/empty tokens to SignalCallbacks.onFailure with ERROR_TOKEN_GENERATION_FAILED = 112.
  • Update/add unit tests covering the new context behavior and null/empty token failure routing.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
ThirdPartyAdapters/unity/unity/src/main/java/com/google/ads/mediation/unity/UnityRewardedAd.java Allow show with non-Activity context by passing nullable Activity to Unity show.
ThirdPartyAdapters/unity/unity/src/main/java/com/google/ads/mediation/unity/UnityInterstitialAd.java Same nullable-Activity show behavior for interstitial.
ThirdPartyAdapters/unity/unity/src/main/java/com/google/ads/mediation/unity/UnityMediationAdapter.java Route null/empty RTB token to onFailure via new RoutingTokenListener and error code 112.
ThirdPartyAdapters/unity/unity/src/test/kotlin/com/google/ads/mediation/unity/UnityRewardedAdTest.kt Update show-context test to expect Unity show called with null activity.
ThirdPartyAdapters/unity/unity/src/test/kotlin/com/google/ads/mediation/unity/UnityInterstitialAdTest.kt Add show-context test to expect Unity show called with null activity.
ThirdPartyAdapters/unity/unity/src/test/kotlin/com/google/ads/mediation/unity/UnityMediationAdapterTest.kt Add tests asserting null/empty tokens route to SignalCallbacks.onFailure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vishaldhiman-unity
vishaldhiman-unity marked this pull request as ready for review July 13, 2026 17:33
@AnwarissaAsbah

Copy link
Copy Markdown

is the following check still needed? In public api migration PR, we were able to remove it

if (adFormat == AdFormat.BANNER && !(rtbSignalData.getContext() instanceof Activity)) {                
   signalCallbacks.onFailure(new AdError(ERROR_CONTEXT_NOT_ACTIVITY,                         
     "Unity Ads RTB Banner ads require activity context", ADAPTER_ERROR_DOMAIN));                  
   return;                                                      
 }

@vishaldhiman-unity

Copy link
Copy Markdown
Author

is the following check still needed? In public api migration PR, we were able to remove it

if (adFormat == AdFormat.BANNER && !(rtbSignalData.getContext() instanceof Activity)) {                
   signalCallbacks.onFailure(new AdError(ERROR_CONTEXT_NOT_ACTIVITY,                         
     "Unity Ads RTB Banner ads require activity context", ADAPTER_ERROR_DOMAIN));                  
   return;                                                      
 }

As discussed will remove thanks.

BannerView accepts a plain Context since Unity Ads SDK 4.16.2, so drop
the Activity checks in collectSignals and the banner load path and pass
the context through directly. Also tighten the RoutingTokenListener
Javadoc to the implemented behavior.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@vishaldhiman-unity

Copy link
Copy Markdown
Author

Good catch on the banner Activity-context check, fixed it in a0ce9fa. BannerView accepts a plain Context since 4.16.2, so removed the Activity requirement from the banner load path too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

vishaldhiman-unity and others added 2 commits July 16, 2026 09:00
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
No longer referenced after dropping the Activity-context requirements.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

…ivity test

Copilot flagged that the test only checked onSuccess() and would still
pass if onFailure or other callbacks were also invoked.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@vishaldhiman-unity
vishaldhiman-unity requested a review from a team July 16, 2026 18:46

@bichenwang bichenwang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm otherwise


static final String ERROR_MSG_NON_ACTIVITY =
"Unity Ads requires an Activity context to load ads.";
/** UnityAds returned no usable bidding token; routed to onFailure so the bidder skips Unity. */

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: format this line

signalCallbacks.onFailure(
new AdError(
ERROR_TOKEN_GENERATION_FAILED,
"Unity Ads returned a null or empty bidding token.",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other errors have a named constant for an error message. should we do the same?

@vishaldhiman-unity
vishaldhiman-unity merged commit 5377ed1 into main Jul 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants