Audience Network 可讓您利用 Facebook 廣告,將 Android 應用程式變成您的營利來源。獎勵式影片廣告能提供全螢幕體驗,讓用戶自行選擇是否要透過觀看影片來取得獎勵(例如虛擬貨幣、應用程式內物品和隱藏內容等)。此廣告體驗長度為 15-30 秒,用戶不可略過,且內附的結束說明卡設有呼籲字句。整段影片播完後,您會收到回呼以將建議的獎勵頒給用戶。
確保您在開始操作前,已經先行參閱 Audience Network 新手指南及 Android 新手指南。
Android Audience Network SDK 5.1 版已加入此方法。
若是 5.3.0
或更高版本,您必須初始化 Android 版 Audience Network SDK如欲了解有關初始化 Android 版 Audience Network SDK 的方法,請參閱此文件。
在建立廣告物件及載入廣告前,請為 Audience Network SDK 執行初始化。我們建議您在啟動應用程式時執行此動作。
public class YourApplication extends Application { ... @Override public void onCreate() { super.onCreate(); // Initialize the Audience Network SDK AudienceNetworkAds.initialize(this); } ... }
在活動頂部加入下列代碼,以匯入 Facebook 廣告 SDK:
import com.facebook.ads.*;
接著,初始化獎勵式影片物件、設定接聽程式,並載入影片廣告創意。獎勵式影片廣告需要使用 RewardedVideoAdListener
介面,這個介面會在範例程式碼中執行下列方法來處理各種事件。舉例來說,在您的活動中:
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 與實體裝置,則可跳過這個步驟。如果您想測試實際廣告,請參閱我們的測試指南。
最後,使用物件的 destroy
方法,在您活動的 onDestroy
方法中清理該物件。請注意,您也應該先使用 destroy
方法來清理舊廣告物件,再將物件指派給新的執行個體,以免記憶體流失。
@Override protected void onDestroy() { if (rewardedVideoAd != null) { rewardedVideoAd.destroy(); rewardedVideoAd = null; } super.onDestroy(); }
Audience Network 中的影片廣告需要硬件加速顯示才能啟動,否則您的影片畫面可能會出現黑屏。這適用於
如果您的 Target 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 社群守則後,然後就可以提交您的應用程式以供審查。