admob奖励视频检查用户是否使用php观看了视频

时间:2020-02-05 17:27:27

标签: android admob

我为Android游戏添加了广告 但必须先通过php验证,才能添加积分 但无论如何我都没有想过 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

“奖励视频”将根据创建“奖励视频广告”单元时提供的配置返回特定值。默认情况下,它设置为 1 。意味着当用户观看奖励视频而未取消奖励视频时,您将从回调中获得 1 。您可以在 onUserEarnedReward 回调方法中调用REST Api endpint(php)。这是Official Docs

的示例实现
myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (rewardedAd.isLoaded()) {
            Activity activityContext = ...;
            RewardedAdCallback adCallback = new RewardedAdCallback() {
                @Override
                public void onRewardedAdOpened() {
                    // Ad opened
                }

                @Override
                public void onRewardedAdClosed() {
                    // Ad closed without completion
                }

                @Override
                public void onUserEarnedReward(@NonNull RewardItem reward) {
                    // User earned reward call the rest api
                }

                @Override
                public void onRewardedAdFailedToShow(int errorCode) {
                    // Ad failed to display.
                }
            };
            rewardedAd.show(activityContext, adCallback);
        } else {
            Log.d("TAG", "The rewarded ad wasn't loaded yet.");
        }
    }
});

Official Integration Docs有几个样机代码可能会帮助您完成操作。按照说明进行操作,就可以了。

警告:应该通过授权保护Rest API端点,否则,很容易受到来自应用程序外部的错误调用的影响。

答案 1 :(得分:0)

Admob 现在支持奖励广告的服务器端验证。 这是 referenceclient-side。这是我从 an article 编辑的示例 PHP 代码。:

<?php
    if(isset($_GET["signature"])) {

        //Get current admob public keys
        $verifier_keys = file_get_contents('https://www.gstatic.com/admob/reward/verifier-keys.json');
        
        if(!$verifier_keys_arr = json_decode($verifier_keys, true)) {
            throw new Exception("Invalid Public Keys");
        } elseif(!is_array($verifier_keys_arr)) {
            throw new Exception("Empty Public Keys");
        }

        $public_key_pem = $verifier_keys_arr['keys'][0]['pem'];

        //Admob sdk will send the query string upon watching ad, just grab them:
        $query_string = $_SERVER['QUERY_STRING'];

        $signature = trim($_GET['signature']);
        // The important thing is the replacement and padding of the signature result string here
        $signature = str_replace(['-', '_'], ['+', '/'], $signature);
        $signature .= '===';

        // The data element string for signing
        $message = substr($query_string, 0, strpos($query_string, 'signature')-1);

        if(openssl_verify($message, $signature, $public_key_pem, OPENSSL_ALGO_SHA256)){
            //verified
            $output = "verified";

            //Get All keys from https://developers.google.com/admob/android/ssv#ssv_callback_parameters
            $reward_item = $_GET['reward_item'];
            $user_id = $_GET['user_id'];
            $custom_data = $_GET['custom_data'];

            //Now do your things after validating the ad response
            //for example
            if($custom_data==="GIVE_COINS") {
                //give coins
            } elseif($custom_data==="LEVEL_UP") {
                //level up
            }

        } else {
            //echo openssl_error_string();
            throw new Exception("Unable to verify the query");
        }
    } else {
        // do nothing, somebody just opened the link
        //echo `Error 404, this page not exists`
    }
?>