简体   繁体   English

如何在 Ionic 6/Capacitor 中使用不同的 admob 方法?

[英]How to use different admob methods in Ionic 6/Capacitor?

I have a ion-tab-bar component and I want to display a BannerAd.我有一个ion-tab-bar组件,我想显示一个 BannerAd。 I used the following library for my ionic app.我为我的离子应用程序使用了以下库。

https://github.com/capacitor-community/admob https://github.com/capacitor-community/admob

To display the add I have to use margin, because the add will overlaps the tab bar.要显示添加我必须使用边距,因为添加将与标签栏重叠。 I have read the doc.我已经阅读了文档。

Margin Banner.边距横幅。 Default is 0px;默认为 0px; If position is BOTTOM_CENTER, margin is be margin-bottom.如果 position 为 BOTTOM_CENTER,则边距为边距底部。 If position is TOP_CENTER, margin is be margin-top.如果 position 为 TOP_CENTER,则边距为边距顶部。

I have two methods to display ads in my app.我有两种方法可以在我的应用中展示广告。 The BannerAd method is only for the pages without the ion-tab-bar . BannerAd方法仅适用于没有ion-tab-bar的页面。 The other for the pages that contains a ion-tab-bar .另一个用于包含ion-tab-bar的页面。 This is my code:这是我的代码:

admob.service.ts admob.service.ts

    import { Injectable } from '@angular/core';
    import { Platform } from '@ionic/angular';
    import { AdMob, BannerAdOptions, BannerAdSize, BannerAdPosition } from '@capacitor-community/admob';
    
    @Injectable({
        providedIn: 'root'
    })
    export class AdMobService {
    
        constructor(private platform: Platform) { }
    
        async bannerAd(admobId: string) {
            const options: BannerAdOptions = {
                adId: admobId,
                adSize: BannerAdSize.FULL_BANNER,
                position: BannerAdPosition.BOTTOM_CENTER,
                margin: 0,
                isTesting: true,
                npa: true
            };
    
            await this.platform.ready();
    
            await AdMob.initialize({
                requestTrackingAuthorization: true,         
                initializeForTesting: true,
            }).catch(err => console.error('Could not init Admob', err));
    
            await AdMob.showBanner(options).catch(err => console.error('Could not init Admob banner', err));
        }
    
        async bannerAdTab(admobId: string) {
            const options: BannerAdOptions = {
                adId: admobId,
                adSize: BannerAdSize.FULL_BANNER,
                position: BannerAdPosition.BOTTOM_CENTER,
                margin: 50,
                isTesting: true,
                npa: true
            };
    
            await this.platform.ready();
    
            await AdMob.initialize({
                requestTrackingAuthorization: true,      
                initializeForTesting: true,
            }).catch(err => console.error('Could not init Admob', err));
    
            await AdMob.showBanner(options).catch(err => console.error('Could not init Admob banner', err));
        }
    }

I use the methods like this is my page:我使用这样的方法是我的页面:

home.page.ts主页.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';
import { AdMobService } from 'src/app/services/admob.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

  constructor(private admob: AdMobService) { }

  ngOnInit() {
    this.admob.bannerAd("admobid");
  }
 
}

And in the tab page like this:在这样的标签页中:

tab.page.ts标签页.ts

import { Component, OnInit } from '@angular/core';
import { AdMobService } from 'src/app/services/admob.service';

@Component({
  selector: 'app-tab',
  templateUrl: './tab.page.html',
  styleUrls: ['./tab.page.scss'],
})
export class TabPage implements OnInit {

  constructor(private admob: AdMobService) { }

  ngOnInit() {
    this.admob.bannerAdTab("AdmobId");
  }
}

The bannerAd method works good but when I navigate to my tabs page it doesn't apply the margin . bannerAd方法效果很好,但是当我导航到我的标签页时,它不应用margin

How can I use the different admob methods in my pages?如何在我的页面中使用不同的 admob 方法?

Second question:第二个问题:

Do I use the admob service in the component or the page where the component is called?我是在组件中使用admob服务还是在调用该组件的页面?

You need to add an event listener for when the banner ad sizes changes, then add your margin to your ion-router-outlet .当横幅广告尺寸发生变化时,您需要添加一个事件监听器,然后将您的边距添加到您的ion-router-outlet

Try something like this:尝试这样的事情:

import { Injectable } from '@angular/core';
    import { Platform } from '@ionic/angular';
    import { AdMob, BannerAdOptions, BannerAdSize, BannerAdPosition, BannerAdPluginEvents } from '@capacitor-community/admob';
    
    @Injectable({
        providedIn: 'root'
    })
    export class AdMobService {
        
        private appMargin:number = 0;

        constructor(private platform: Platform) {
            this.init();
        }
        
        async init() {

           await this.platform.ready();

           await AdMob.initialize({
                requestTrackingAuthorization: true,         
                initializeForTesting: true,
           }).catch(err => console.error('Could not init Admob', err));

           this.eventOnAdSize = AdMob.addListener(BannerAdPluginEvents.SizeChanged, (info: any) => {
            // Subscribe Change Banner Size
            this.appMargin = parseInt(info.height, 10);
            if (this.appMargin > 0) {
                const app: HTMLElement = document.querySelector('ion-router-outlet');
                app.style.marginBottom = this.appMargin + 'px';
            }
        });
        }

        async bannerAd(admobId: string) {
            const options: BannerAdOptions = {
                adId: admobId,
                adSize: BannerAdSize.FULL_BANNER,
                position: BannerAdPosition.BOTTOM_CENTER,
                margin: 0,
                isTesting: true,
                npa: true
            };
       
            await AdMob.showBanner(options).catch(err => console.error('Could not init Admob banner', err));
        }
    
        async bannerAdTab(admobId: string) {
            const options: BannerAdOptions = {
                adId: admobId,
                adSize: BannerAdSize.FULL_BANNER,
                position: BannerAdPosition.BOTTOM_CENTER,
                margin: 50,
                isTesting: true,
                npa: true
            };
        
            await AdMob.showBanner(options).catch(err => console.error('Could not init Admob banner', err));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM