Audience Network 可讓您利用 Facebook 廣告,將 Android 應用程式變成您的獲利來源。擁有全螢幕體驗的獎勵式影片廣告,可讓用戶自行選擇是否要透過觀看影片來取得獎勵(例如虛擬貨幣、應用程式內物品和隱藏內容等)。廣告體驗長達 15-30 秒,不可中途略過且會展示附有行動呼籲按鈕的結束圖卡。用戶看完整段影片後,您會收到授予用戶建議獎勵的回呼。
繼續操作前,請務必詳閱 Audience Network 新手指南與 Android 新手指南。
Android Audience Network SDK 5.1 版本已新增此方法。
自 5.3.0
及以上版本起,必須明確初始化 Audience Network Android SDK。請參閱本文件瞭解如何初始化 Audience Network Android SDK。
在建立廣告物件或載入廣告之前,應初始化 Audience Network SDK。建議在應用程式啟動時使用此方法。
public class YourApplication extends Application { ... @Override public void onCreate() { super.onCreate(); // Initialize the Audience Network SDK AudienceNetworkAds.initialize(this); } ... }
在 Activity 頂部加入下列程式碼,以匯入 Facebook 廣告 SDK:
import com.facebook.ads.*;
接著初始化獎勵式影片物件,設定接聽程式並載入影片廣告創意。獎勵式影片廣告需要 RewardedVideoAdListener
介面來實作下列範例程式碼中的方法,以處理各種不同的事件。以下列 Activity 為例:
private final String TAG = RewardedVideoAdActivity.class.getSimpleName(); private RewardedVideoAd rewardedVideoAd; @Override public void onCreate(Bundle savedInstanceState) { ... // Instantiate a RewardedVideoAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get // a no fill error). rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID"); RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { @Override public void onError(Ad ad, AdError error) { // Rewarded video ad failed to load Log.e(TAG, "Rewarded video ad failed to load: " + error.getErrorMessage()); } @Override public void onAdLoaded(Ad ad) { // Rewarded video ad is loaded and ready to be displayed Log.d(TAG, "Rewarded video ad is loaded and ready to be displayed!"); } @Override public void onAdClicked(Ad ad) { // Rewarded video ad clicked Log.d(TAG, "Rewarded video ad clicked!"); } @Override public void onLoggingImpression(Ad ad) { // Rewarded Video ad impression - the event will fire when the // video starts playing Log.d(TAG, "Rewarded video ad impression logged!"); } @Override public void onRewardedVideoCompleted() { // Rewarded Video View Complete - the video has been played to the end. // You can use this event to initialize your reward Log.d(TAG, "Rewarded video completed!"); // Call method to give reward // giveReward(); } @Override public void onRewardedVideoClosed() { // The Rewarded Video ad was closed - this can occur during the video // by closing the app, or closing the end card. Log.d(TAG, "Rewarded video ad closed!"); } }; rewardedVideoAd.loadAd( rewardedVideoAd.buildLoadAdConfig() .withAdListener(rewardedVideoAdListener) .build()); ... }
private RewardedVideoAd rewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Instantiate a RewardedVideoAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get // a no fill error). rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID"); RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { ... @Override public void onAdLoaded(Ad ad) { // Rewarded video ad is loaded and ready to be displayed rewardedVideoAd.show(); } ... }; rewardedVideoAd.loadAd( rewardedVideoAd.buildLoadAdConfig() .withAdListener(rewardedVideoAdListener) .build()); ... }
如果廣告載入之後沒有立即顯示,開發人員有責任檢查廣告是否已失效。廣告載入成功後,有效期為 60 分鐘。如果顯示的是已失效的廣告,您將無法獲得付款。您應呼叫 isAdInvalidated()
來驗證廣告。
您應該遵循以下想法,但請不要將程式碼複製到專案中,因為它只是範例:
private RewardedVideoAd rewardedVideoAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Instantiate a RewardedVideoAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get // a no fill error). rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID"); RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { ... }; // load the ad rewardedVideoAd.loadAd( rewardedVideoAd.buildLoadAdConfig() .withAdListener(rewardedVideoAdListener) .build()); // Here is just an example for displaying the ad with delay // Please call this method at appropriate timing in your project showAdWithDelay(); } private void showAdWithDelay() { /** * Here is an example for displaying the ad with delay; * Please do not copy the Handler into your project */ // Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { // Check if rewardedVideoAd has been loaded successfully if (rewardedVideoAd == null || !rewardedVideoAd.isAdLoaded()) { return; } // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad. if (rewardedVideoAd.isAdInvalidated()) { return; } rewardedVideoAd.show(); } }, 1000 * 60 * 15); // Show the ad after 15 minutes }
如果使用預設的 Google Android 模擬器,您必須在載入測試廣告前新增下行程式碼:AdSettings.addTestDevice("HASHED ID");
。
首次發出在裝置上載入廣告的要求時,請使用列印至 logcat 的雜湊編號。
Genymotion 和實體裝置無須進行這個步驟。如果想要使用真實廣告進行測試,請參閱測試指南。
最後在您 Activity 的 onDestroy
方法中,使用其 destroy
方法來清除物件。請注意,將 destroy
方法指派至新的執行個體時,您也應使用此方法來清除舊的廣告物件,以免記憶體流失。
@Override protected void onDestroy() { if (rewardedVideoAd != null) { rewardedVideoAd.destroy(); rewardedVideoAd = null; } super.onDestroy(); }
Audience Network 中的影片廣告必須啟用硬體加速顯示,否則觀看影片時可能會出現畫面變黑的情況。適用項目
如果目標 API 層級 >=14(Ice Cream Sandwich,Android 4.0.1),預設會啟用硬體加速,但也可在應用程式層級或活動層級明確啟用這個功能。
在 Android Manifest 檔案中,將以下屬性加入 <application>
標籤,即可啟用整個應用程式的硬體加速:
<application android:hardwareAccelerated="true" ...>
如果只想針對應用程式中的特定活動啟用這個功能,可以在 Android Manifest 檔案中將以下功能加入 <activity>
標籤。以下範例會針對 AudienceNetworkActivity
啟用硬體加速,這是用於顯示插頁廣告和獎勵式影片:
<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" .../>
您可自行選擇是否執行此步驟;使用獎勵式影片廣告時,不一定要實作伺服器端獎勵驗證。若您決定藉由在自己的伺服器實行獎勵驗證步驟以提高安全性,才需實作此程序。請將發佈商端點提供給您的 Facebook 業務代表,以啟用此功能。
若您負責管理用戶獎勵的伺服器端,可參考 Facebook 提供的解決方案,透過驗證技術安全地執行此流程。我們的伺服器會與指定的 https 端點通訊,以驗證各次廣告曝光,確認是否應授予獎勵。
onRewardServerSuccess
- 若在步驟 3 收到 200 回應便會觸發此事件。onRewardServerFailed
- 若在步驟 3 收到非 200 回應便會觸發此事件。以下網址範例會從 Facebook 伺服器叫用發佈商端點:https://www.your_end_point.com/?token=APP_SECRET&puid=USER_ID&pc=REWARD_ID&ptid=UNIQUE_TRANSACTION_ID
工作流程如下所示:
初始化獎勵式影片物件後,您需要在廣告載入前,將用戶編號和獎勵金額傳入獎勵式廣告資料;用戶編號及獎勵金額皆為字串,例如:
private RewardedVideoAd rewardedVideoAd; private void loadRewardedVideoAd { // Instantiate a RewardedVideoAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get // a no fill error). rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID"); RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { ... }; // Create the rewarded ad data RewardData rewardData = new RewardData("YOUR_USER_ID", "YOUR_REWARD"); rewardedVideoAd.loadAd( rewardedVideoAd.buildLoadAdConfig() .withAdListener(rewardedVideoAdListener) .withRewardData(rewardData) .build()); }
您必須實作 S2SRewardedVideoAdListener
介面,應用程式才能收到通知,得知獎勵的驗證結果。這包括所有在 RewardedVideoAdListener
介面上方的事件通知,以及額外兩項事件。以下事件可與上述事件搭配使用。
@Override public void onRewardServerSuccess() { // Rewarded video ad validated } @Override public void onRewardServerFailed() { // Rewarded video ad not validated or no response from server }
請注意,伺服器驗證回呼可能在用戶關閉結束圖卡後才會發生。在收到回呼之前,您不應將獎勵式影片物件解除配置。
參考 Github 上的 Audience Network Android 程式碼範例,將專案匯入到您的整合開發環境(IDE),然後在裝置或模擬器上執行。
當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策與 Facebook 社群守則,接著便可以將應用程式送審。