Ionic 如何使用不同的admob方法在离子6/电容器?

5anewei6  于 2022-12-16  发布在  Ionic
关注(0)|答案(1)|浏览(181)

我有一个ion-tab-bar组件,我想显示一个BannerAd。我为我的离子应用程序使用了以下库。
https://github.com/capacitor-community/admob
要显示添加我必须使用边距,因为添加将重叠标签栏。我已经阅读了文档。
边距横幅。默认值为0 px;如果位置为BOTTOM_CENTER,则边距为下边距。如果位置为TOP_CENTER,则边距为上边距。
我有两个方法在应用中显示广告。BannerAd方法仅适用于没有ion-tab-bar的页面。另一个方法适用于包含ion-tab-bar的页面。以下是我的代码:

广告服务.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));
        }
    }

我使用的方法就像这是我的网页:

主页.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");
  }
 
}

在这样的标签页中:

选项卡.页码.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");
  }
}

bannerAd方法工作正常,但当我导航到我的标签页时,它不应用margin

如何在我的页面中使用不同的admob方法?
第二个问题:
我是在组件中还是在调用组件的页面中使用admob服务?

mm5n2pyu

mm5n2pyu1#

您需要添加一个事件侦听器,以便在横幅广告大小发生变化时进行侦听,然后将边距添加到ion-router-outlet
试试这样的方法:

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));
        }
    }

相关问题