当前位置: 首页 > 知识库问答 >
问题:

Facebook受众网络仅在启用测试时显示广告,但在禁用测试选项时不显示广告

秦博达
2023-03-14

我已经完成了在我的应用程序中显示广告的Facebook Audience Network实施的所有过程。我还创建了一个由谷歌团队批准的google admob帐户。我还选择了Facebook Audience Network作为广告源,在google admob帐户的中介页面下进行竞标,并在google admob的中介页面中添加了我的facebook业务帐户的放置ID、用户令牌和属性ID。

问题:当在facebook受众网络测试页面启用测试时,我会收到广告。但当我在facebook受众网络测试页面禁用测试时,我不会收到广告(Facebook Audience网络测试页面链接:https://business.facebook.com/pub/testing?business_id=XXXXXXXXXXX)

广告类型:本地广告

我在我的回收器视图加载广告与回收器适配器java类的帮助

我的适配器类代码:

public class NewRecentPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements ActivityCompat.OnRequestPermissionsResultCallback, AudienceNetworkAds.InitListener{

    private String TAG="NewRecentPostAdapter";
    private Context context;
    private Activity activity;
    //    private List<RecentPostsResponse.Data> recentList;
    private List<Object> recentList;
    private List<Object> finalList;
    private ArrayList<NativeAd> nativeAd = new ArrayList<>();
    private final int ITEM_TYPE_DATA = 0;
    private final int ITEM_TYPE_AD = 1;
    private final int AD_POSITION = 1;
    private final int AD_POSITION_EVERY_COUNT = 5;
    private NativeAdsManager fbNativeManager;
    private static final String  FB_NATIVE_AD_ID = "XXXXXXXXXX";

    public void initNativeAds(){
        MobileAds.initialize(context, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
                for (String adapterClass : statusMap.keySet()) {
                    AdapterStatus status = statusMap.get(adapterClass);
                    Log.d("MyApp", String.format(
                            "Adapter name: %s, Description: %s, Latency: %d",
                            adapterClass, status.getDescription(), status.getLatency()));
                }

                // Start loading ads here...
                loadingAds();
            }
        });
    }

    public void loadingAds(){
        fbNativeManager.setListener(new NativeAdsManager.Listener() {
            @Override
            public void onAdsLoaded() {
                Log.i(TAG, "onAdsLoaded!" + fbNativeManager.getUniqueNativeAdCount());
                int count = fbNativeManager.getUniqueNativeAdCount();
                for(int i=0; i< count; i ++) {
                    NativeAd ad = fbNativeManager.nextNativeAd();
                    addNativeAd(i, ad);
                }
            }
            @Override
            public void onAdError(AdError adError) {
                Log.i(TAG, "AdError: " + adError.getErrorMessage());
            }
        });
        Log.i(TAG, "isLoaded: " + fbNativeManager.isLoaded());
        fbNativeManager.loadAds();
    }
    public void addNativeAd(int i, NativeAd ad) {
        if (ad == null) {
            return;
        }
        this.nativeAd.add(i, ad);
        if(recentList.size()>0){
            finalList = (List<Object>) recentList.get(0);
            if(finalList.size()>0){
                this.finalList.add(ad);
            }
        }
        notifyDataSetChanged();
    }

    public NewRecentPostAdapter(Activity activity,Context context, List<Object> recentList) {
        this.activity = activity;
        this.context = context;
        this.recentList = recentList;
        fbNativeManager = new NativeAdsManager(context, FB_NATIVE_AD_ID,1);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == ITEM_TYPE_AD)
        {
            View v = LayoutInflater.from(context).inflate(R.layout.native_ads, parent, false);
            return new NewRecentPostAdapter.NativeAdViewHolder(v);
        }else {
            View v = LayoutInflater.from(context).inflate(R.layout.recent_posts_adapter, parent, false);
            return new NewRecentPostAdapter.PostViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            finalList = (List<Object>) recentList.get(0);
            Object item1 = finalList.get(position);
            NativeAd ad = null;
            boolean isAd = true;
            try {
                RecentAllPostResponse.Data item = (RecentAllPostResponse.Data) item1;
                isAd=false;
            } catch (Exception e) {
                isAd = true;
                ad = (NativeAd) item1;
                e.printStackTrace();
            }

            if(!isAd) {
                RecentAllPostResponse.Data item = (RecentAllPostResponse.Data) item1;
                Log.v(TAG,"FF="+item);
                if (!item.getTitle().isEmpty()){
                    ((NewRecentPostAdapter.PostViewHolder) holder).title.setText(item.getTitle());
                    ((NewRecentPostAdapter.PostViewHolder) holder).title.setSelected(true);
                }else {
                    ((NewRecentPostAdapter.PostViewHolder) holder).title.setText("NA");
                }
                if (!item.getPrice().isEmpty()){
                    NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
                    ((NewRecentPostAdapter.PostViewHolder) holder).price.setText(format.format(Integer.parseInt(item.getPrice())));
                }else {
                    ((NewRecentPostAdapter.PostViewHolder) holder).price.setText("NA");
                }
                if (!item.getLocation().isEmpty()){
                    ((NewRecentPostAdapter.PostViewHolder) holder).location.setText(item.getLocation());
                }else {
                    ((NewRecentPostAdapter.PostViewHolder) holder).location.setText("NA");
                }

                if (!TextUtils.isEmpty(item.getImage())) {
                    if (item.getImage().contains(",")){
                        String[] splitImage = item.getImage().split(",");
                        Glide.with(context)
                                .load(ImageURL.POST_IMG_PATH+splitImage[0])
                                .fitCenter()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(ContextCompat.getDrawable(context,R.drawable.amo_logo))
                                .into(((NewRecentPostAdapter.PostViewHolder) holder).image);
                    }else {
                        Glide.with(context)
                                .load(ImageURL.POST_IMG_PATH+item.getImage())
                                .fitCenter()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(ContextCompat.getDrawable(context,R.drawable.amo_logo))
                                .into(((NewRecentPostAdapter.PostViewHolder) holder).image);
                    }
                } else {
                    Glide.with(context)
                            .load(R.drawable.picture)
                            .fitCenter()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .placeholder(ContextCompat.getDrawable(context,R.drawable.amo_logo))
                            .into(((NewRecentPostAdapter.PostViewHolder) holder).image);
                }
                holder.itemView.setOnClickListener(view -> {
                    FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
                    DetailFragment detailFragment = DetailFragment.newInstance(item.getId(), item.getCatId(), item.getBreedId(), item.getAdId(), item.getPostId(), item.getTitle(), item.getDescription(),
                            item.getLocation(), item.getPostDate(), item.getPostBy(), item.getPostProfile(), item.getPostContact(), item.getGender(), item.getColor(), item.getWeight(),
                            item.getHeight(), item.getAge(), item.getPrice(), item.getImage(), item.getSince(), item.getFollowing(), item.getFollowers(),item.getVideo_url());
                    ft.replace(R.id.container, detailFragment);
                    ft.addToBackStack(null);
                    ft.commit();
                });

                ((NewRecentPostAdapter.PostViewHolder) holder).call_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // reuqest for permission
                            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CALL_PHONE}, amo.REQUEST_CALL);
                        } else {
                            boolean loggedIn = Prefs.getBoolean("isLoggedIn", false);
                            if (loggedIn){
                                String contact = item.getPostContact();
                                if (!contact.isEmpty() && !contact.equalsIgnoreCase("NA")){
                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact));
                                    context.startActivity(intent);
                                }else {
                                    amo.showShortSnack(((NewRecentPostAdapter.PostViewHolder) holder).main_lay,"Contact No is Not Valid");
                                }
                            }else {
                                amo.showShortSnack(((NewRecentPostAdapter.PostViewHolder) holder).main_lay,context.getResources().getString(R.string.you_reg_proceed));
                            }
                        }
                    }
                });
                ((NewRecentPostAdapter.PostViewHolder) holder).chat_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean loggedIn = Prefs.getBoolean("isLoggedIn", false);
                        if (loggedIn){

                            String postid = item.getPostId();
                            amo.firebase(postid,context,activity);
                        }else {
                            amo.showShortSnack(((NewRecentPostAdapter.PostViewHolder) holder).main_lay,context.getResources().getString(R.string.you_reg_chat));
                        }
                    }
                });

                Log.v(TAG,"FF="+fbNativeManager.isLoaded());
            }else{
                View mNativeView = NativeAdView.render(context, ad, NativeAdView.Type.HEIGHT_300);

                AdView.AdViewLoadConfig loadAdConfig = adView.buildLoadAdConfig()
                        .build();

                adView.loadAd(loadAdConfig);
                ((NewRecentPostAdapter.NativeAdViewHolder) holder).nativeAdContainer.removeAllViews();
                ((NewRecentPostAdapter.NativeAdViewHolder) holder).nativeAdContainer.addView(mNativeView);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 123: {
                for (int i = 0, len = permissions.length; i < len; i++) {
                    String permission = permissions[i];
                    if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        // user rejected the permission
                        boolean showRationale = shouldShowRequestPermissionRationale(activity, permission);
                        if (!showRationale) {
                            // user also CHECKED "never ask again"
                            // you can either enable some fall back,
                            // disable features of your app
                            // or open another dialog explaining
                            // again the permission and directing to
                            // the app setting
                            Log.v("CallOption", "Permission Denied1");
                        } else if (Manifest.permission.CALL_PHONE.equals(permission)) {
                            Log.v("CallOption", "Permission Denied2");
                            // user did NOT check "never ask again"
                            // this is a good place to explain the user
                            // why you need the permission and ask if he wants
                            // to accept it (the rationale)
                        }
                    }
                }
                return;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(finalList != null && finalList.size()>0 &&
                finalList.get(position) instanceof NativeAd)
            return ITEM_TYPE_AD;
        else
            return ITEM_TYPE_DATA;
    }

    @Override
    public int getItemCount() {
        finalList = (List<Object>) recentList.get(0);
        if(finalList == null)
            return 0;
        return finalList.size();
    }

    @Override
    public void onInitialized(AudienceNetworkAds.InitResult initResult) {

    }

    public class PostViewHolder extends RecyclerView.ViewHolder {
        private AppCompatImageView image;
        private ConstraintLayout main_lay;
        private NoboButton call_btn,chat_btn;
        private AppCompatTextView title, price, location;
        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            main_lay = itemView.findViewById(R.id.main_lay);
            image = itemView.findViewById(R.id.rec_posts_image);
            title = itemView.findViewById(R.id.rec_posts_title);
            price = itemView.findViewById(R.id.rec_posts_price);
            location = itemView.findViewById(R.id.rec_posts_location);
            call_btn = itemView.findViewById(R.id.call_btn);
            chat_btn = itemView.findViewById(R.id.chat_btn);
        }
    }

    public static class NativeAdViewHolder extends RecyclerView.ViewHolder {
        LinearLayout nativeAdContainer;
        NativeAdViewHolder(@NonNull View itemView) {
            super(itemView);
            nativeAdContainer = itemView.findViewById(R.id.adContainer);
        }
    }
}

使用MediationTestSuite时,它显示错误,我在这里附上错误截图供您参考。

帮我解决这个问题

谢谢

共有1个答案

万知
2023-03-14

谢谢你的帮助。我自己解决了这个问题。我所做的是,我将“Facebook原生广告源代码”替换为“谷歌原生广告源代码”。现在它正在通过竞价过程展示facebook广告。

  1. 在FAN(Facebook观众网络)中完成竞标过程

注意:只有从google playstore下载应用程序时,才会显示原始广告

 类似资料:
  • 这是我的 .Gradle文件 这是主要活动

  • 我已经在我的手机上测试了广告功能。只显示测试广告。当我用我从AdMob得到的单位id替换时,真正的广告就不显示了。 问题出在哪里? 下面是我的代码: 对于我使用的xml 用于加载我使用的ad 在manifeast我有 ...。 但我的广告不加载在android设备上。测试广告运作良好。所有这些操作我都用了一个真实的装置。有什么需要帮忙的吗?

  • 它在模拟器和真实设备上显示测试广告。但是当我把广告ID改成我的真实ID时,它就不起作用了。。我的应用程序也在AppStore中,但我没有推送更新。我只在Xcode中添加了AdMob,然后连接了真正的设备并在其上进行了测试。我必须将AdMob的最新更新发布到App Store才能看到真正的广告吗?我该怎么办?在推送更新之前,我想测试一下我的真实广告。。 (已填写付款明细)

  • 我已经与Admob广告斗争了将近一周,我在互联网上搜索了所有可能的解决方案,但都没有奏效。我的Android应用程序不显示测试广告也不显示实时广告。这是日志: 我的测试间隙负载没有问题,带电的没有。横幅没有显示,测试的和带电的都没有显示。 这是我的xml AdView: 我在横幅上加了: 活动间隙是以这种方式加载的,它是有效的(测试一没有): 我尝试过的事情 > 使用 使用Firebase广告编译

  • 这是我的第一个Android应用程序,我试图使用admob广告为我的应用程序,但它是不工作,尽管我所做的一切。 这是我活动中与广告相关的代码 下面是我的XML代码 在模拟器上运行应用程序时的logcat读数既奇怪又令人困惑 如您所见,logcat中至少显示了3个错误。首先,google play服务已经导入到项目中。我可以在android私有库文件夹中看到它(但不是在libs文件夹中)。 还有一个

  • 我已经在我的android应用中实现了facebook受众网络横幅广告。我想在用户点击它并返回到应用程序后15秒不播放广告。我已经搜索了很多,没有找到任何解决方案,我这样做是为了防止非法点击。我怎么能这么做?