ad mob policy issue

20 Views Asked by At

Im getting this issue can someone solve this by looking the code Disallowed Rewarded Implementation Must fix Rewarded ads may only be served after a user affirmatively opts to view a rewarded ad. Additionally, a user must opt to view each rewarded ad individually and cannot opt-in to automatically seeing rewarded ads.

this is the code where ads are initilized

import static android.content.ContentValues.TAG;

public class comp extends AppCompatActivity {

compAdapter compAdapter1;
RewardedAd rewardedAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_afl);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.trans));
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    Button back = findViewById(R.id.backm);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(comp.this, notesss.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
        }
    });



    SharedPreferences sharedPreferences = getSharedPreferences("MyAppPreferences", MODE_PRIVATE);
    int selectedYear = sharedPreferences.getInt("selectedYear", 0); // 0 is the default value if the key is not found

    if (selectedYear == 1) {
        loadADS();
        RecyclerView EErec = findViewById(R.id.aflN);
        EErec.setLayoutManager(new LinearLayoutManager(this));
        FirebaseRecyclerOptions<compM> options =
                new FirebaseRecyclerOptions.Builder<compM>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("firstYr").child("nanosc")
                                , compM.class)
                        .build();
        compAdapter1 = new compAdapter(options, new BtnClick() {
            @Override
            public void btnClick(String open) {
                showADS(open);
            }
        });
        EErec.setAdapter(compAdapter1);
    }
    else if (selectedYear == 2) {
        loadADS();
        RecyclerView EErec = findViewById(R.id.aflN);
        EErec.setLayoutManager(new LinearLayoutManager(this));
        FirebaseRecyclerOptions<compM> options =
                new FirebaseRecyclerOptions.Builder<compM>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("secondYr").child("compA")
                                , compM.class)
                        .build();
        compAdapter1 = new compAdapter(options, new BtnClick() {
            @Override
            public void btnClick(String open) {
                showADS(open);
            }
        });
        EErec.setAdapter(compAdapter1);
    } else if (selectedYear == 3) {
        loadADS();
        RecyclerView EErec = findViewById(R.id.aflN);
        EErec.setLayoutManager(new LinearLayoutManager(this));
        FirebaseRecyclerOptions<compM> options =
                new FirebaseRecyclerOptions.Builder<compM>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("thirdYr").child("spm")
                                , compM.class)
                        .build();
        compAdapter1 = new compAdapter(options, new BtnClick() {
            @Override
            public void btnClick(String open) {
                showADS(open);
            }
        });
        EErec.setAdapter(compAdapter1);
    }
}
private void showADS(String open) {
    boolean userOptedIn = true; // Set this value based on user action.

    if (userOptedIn) {
        if (rewardedAd != null) {
            Activity activityContext = this;
            rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
                @Override
                public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                    openLink(open);
                    // Load the next rewarded ad after the user has earned the reward.
                    loadADS();
                }
            });
            rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                @Override
                public void onAdDismissedFullScreenContent() {
                    super.onAdDismissedFullScreenContent();
                    rewardedAd = null;
                    openLink(open);
                    // Load the next rewarded ad after the current ad is dismissed.
                    loadADS();
                }

                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    super.onAdFailedToShowFullScreenContent(adError);
                    Log.d(TAG, "The rewarded ad wasn't ready yet.");
                    rewardedAd = null;
                    openLink(open);
                    // Load the next rewarded ad after the current ad failed to show.
                    loadADS();
                }
            });
        } else {
            // If the rewarded ad is not loaded, display the content without showing the ad.
            Toast.makeText(comp.this, "opening..", Toast.LENGTH_SHORT).show();
            openLink(open);
        }
    } else {
        // Handle the case when the user has not opted to watch a rewarded ad.
        // Optionally, you can display a message or take other actions.
        openLink(open);
    }
}


private void openLink(String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    startActivity(intent);
}
private void loadADS() {
    AdRequest adRequest = new AdRequest.Builder().build();
    String rewardedAdId = getString(R.string.ads);
    RewardedAd.load(comp.this, rewardedAdId, adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            // Handle the error
        }
        @Override
        public void onAdLoaded(@NonNull RewardedAd ad) {
            rewardedAd = ad;
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    compAdapter1.startListening();
}
@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}

}

0

There are 0 best solutions below