דרישות מוקדמות
- Google Mobile Ads SDK מגרסה 19.7.0 ואילך.
- מבצעים את ההוראות שמופיעות במדריך לתחילת העבודה.
ביצוע בדיקות תמיד באמצעות מודעות בדיקה
כשיוצרים ובודקים את האפליקציות, חשוב להשתמש במודעות בדיקה ולא במודעות פעילות בסביבת הייצור. אם לא תעשה זאת, ייתכן שהחשבון שלך יושעה.
הדרך הקלה ביותר לטעון מודעות בדיקה היא באמצעות המזהה הייעודי של יחידת מודעות לבדיקה למודעות מתגמלות ב-Android:
ca-app-pub-3940256099942544/5224354917
היא הוגדרה במיוחד להחזרת מודעות בדיקה לכל בקשה, וניתן להשתמש בה באפליקציות שלכם בזמן תכנות, בדיקה וניפוי באגים. הקפידו להחליף אותה במזהה יחידת המודעות שלכם לפני פרסום האפליקציה.
רוצים לדעת איך מודעות הבדיקה ב-Mobile Ads SDK פועלות במאמר מודעות בדיקה?
Load a rewarded ad object
Rewarded ads are loaded by calling the static load()
method on the
RewardedAd
class and passing in a RewardedAdLoadCallback
. This is usually
done in the onCreate()
method of an Activity
.
Notice that like other format load callbacks, RewardedAdLoadCallback
leverages LoadAdError
to provide higher fidelity error details.
Java
import com.google.android.gms.ads.rewarded.RewardedAd;
public class MainActivity extends Activity {
private RewardedAd rewardedAd;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
rewardedAd = ad;
Log.d(TAG, "Ad was loaded.");
}
});
}
}
Kotlin
class MainActivity : AppCompatActivity() {
private var rewardedAd: RewardedAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adRequest = AdRequest.Builder().build()
RewardedAd.load(this,"ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedAd = null
}
override fun onAdLoaded(ad: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
rewardedAd = ad
}
})
}
}
Set the FullScreenContentCallback
The FullScreenContentCallback
handles events related to displaying your
RewardedAd
. Before you show your RewardedAd
, make sure to set the callback
like so:
Java
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
Kotlin
rewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.")
rewardedAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
rewardedAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
Show the ad
When you show a rewarded ad, you will use an OnUserEarnedRewardListener
object
to handle reward events.
Java
if (rewardedAd != null) {
Activity activityContext = MainActivity.this;
rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.");
}
Kotlin
rewardedAd?.let { ad ->
ad.show(this, OnUserEarnedRewardListener { rewardItem ->
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
Log.d(TAG, "User earned the reward.")
})
} ?: run {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
}
[Optional] Validate server-side verification (SSV) callbacks
Apps that require extra data in server-side
verification callbacks should use the
custom data feature of rewarded ads. Any string value set on a rewarded ad
object is passed to the custom_data
query parameter of the SSV callback. If no
custom data value is set, the custom_data
query parameter value won't be
present in the SSV callback.
The following code sample demonstrates how to set custom data on a rewarded ad object before requesting an ad.
Java
RewardedAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedAdLoadCallback() {
@Override
public void onAdLoaded(RewardedAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedAd = ad;
ServerSideVerificationOptions options = new ServerSideVerificationOptions
.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build();
rewardedAd.setServerSideVerificationOptions(options);
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
});
Kotlin
RewardedAd.load(this, "ca-app-pub-3940256099942544/5354046379",
AdRequest.Builder().build(), object : RewardedAdLoadCallback() {
override fun onAdLoaded(ad: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
val options = ServerSideVerificationOptions.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build()
rewardedAd.setServerSideVerificationOptions(options)
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedAd = null
}
})
If you want to set the custom reward string, you must do so before showing the ad.
שאלות נפוצות
- האם יש זמן קצוב לתפוגה של קריאת האתחול?
- אחרי 10 שניות, Google Mobile Ads SDK מפעיל את
OnInitializationCompleteListener
גם אם האתחול של הרשת לבחירת רשת עדיין לא הושלם. - מה קורה אם רשתות מסוימות בתהליך בחירת הרשת לא מוכנות אחרי שאקבל את הקריאה החוזרת מהאתחול?
מומלץ לטעון מודעה בקריאה החוזרת (callback) של
OnInitializationCompleteListener
. גם אם רשת מסוימת של תהליך בחירת הרשת לא מוכנה, ה-SDK של מודעות Google לנייד עדיין יבקש מהרשת הזו להציג מודעה. כך שאם האתחול של רשת בתהליך בחירת הרשת מסתיים לאחר הזמן הקצוב לתפוגה, היא עדיין יכולה לטפל בבקשות עתידיות להצגת מודעות באותה הפעלה.אפשר להמשיך ולבחון את סטטוס האתחול של כל המתאמים במהלך סשן האפליקציה בקריאה ל-
MobileAds.getInitializationStatus()
.- איך אפשר לברר למה רשת מסוימת של תהליך בחירת הרשת לא מוכנה?
ב
AdapterStatus.getDescription()
מוסבר למה המתאם לא מוכן לטפל בבקשות להצגת מודעות.- האם הקריאה החוזרת (callback) ב-
onUserEarnedReward()
תמיד מתבצעת לפני הקריאה החוזרת ב-onAdDismissedFullScreenContent()
? במודעות Google, כל הקריאות מסוג
onUserEarnedReward()
מתבצעות לפניonAdDismissedFullScreenContent()
. במודעות שמוצגות באמצעות תהליך בחירת הרשת (Mediation), הזמנת הקריאה החוזרת (callback) נקבעת על ידי הטמעת ה-SDK של רשת המודעות של הצד השלישי. בערכות SDK של רשתות מודעות שמספקות קריאה חוזרת (callback) יחידה עם מידע על התגמול, המתאם של תהליך בחירת הרשת מפעיל אתonUserEarnedReward()
לפניonAdDismissedFullScreenContent()
.
דוגמאות ב-GitHub
השלבים הבאים
נסו את הנושאים הבאים: