Admob 20.1.0 中从未调用过 onUserEarnedReward

onUserEarnedReward is never called in Admob 20.1.0

我正在努力将我的 Kotlin 应用程序转换为 Admob 20.1.0。 运行 遇到集成激励广告的问题。

问题是 onUserEarnedReward 从未被调用过。我目前无法用他们解锁的内容奖励用户。我下面的代码放在 AppCompatActivity()

中的 onClickListener
if (mRewardedAd != null) {
    mRewardedAd?.show(this, OnUserEarnedRewardListener { // Redundant SAM-constructor 
        fun onUserEarnedReward(rewardItem: RewardItem) { // Function "onUserEarnedReward" is never used 
            
            val rewardAmount = rewardItem.amount
            val rewardType = rewardItem.type
            println("======================= TYPE -  $rewardType /// AMOUNT - $rewardAmount")
            // This never gets called.
        }
    })
} else {
    println("=======================The rewarded ad wasn't ready yet.")
}

我的导入:

import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
import com.google.android.gms.ads.OnUserEarnedRewardListener

为什么我得到 冗余 SAM 构造函数 并且 从未使用函数“onUserEarnedReward”

原因是您在高阶函数中创建了一个函数,而不是调用该函数。 请尝试使用以下代码。它会起作用

       mRewardedAd?.show(this, OnUserEarnedRewardListener { rewardItem ->
        val rewardAmount = rewardItem.amount
        val rewardType = rewardItem.type
        println("======================= TYPE -  $rewardType /// AMOUNT - $rewardAmount")
      }

为了您更好地理解高阶函数,下面的代码也是可行的

   mRewardedAd?.show(this, OnUserEarnedRewardListener { 
    fun onUserEarnedReward(rewardItem: RewardItem) { 
        val rewardAmount = rewardItem.amount
        val rewardType = rewardItem.type
        println("======================= TYPE -  $rewardType /// AMOUNT - $rewardAmount")
    }
    onUserEarnedReward(it)
})