Angular Ionic 3本地通知推送到特定页面的次数过多

Angular Ionic 3本地通知推送到特定页面的次数过多,angular,cordova,typescript,ionic-framework,Angular,Cordova,Typescript,Ionic Framework,我是爱奥尼亚的新手,我可能错过了一些重要的细节,开发一个简单的应用程序,应该通过打开本地通知来显示特定的页面。问题是:我制作了一个按钮,通过点击它来生成本地通知。如果我第一次生成通知,然后单击它,它就像一个符咒,但是当我生成第二个通知时,如果我单击它,应用程序会推两次新页面,第三次推三次,依此类推,即使我通过导航栏返回根页面 有人能帮我理解我做错了什么? 多谢各位 这是根页面的代码 import { Component } from '@angular/core'; import { NavCo

我是爱奥尼亚的新手,我可能错过了一些重要的细节,开发一个简单的应用程序,应该通过打开本地通知来显示特定的页面。问题是:我制作了一个按钮,通过点击它来生成本地通知。如果我第一次生成通知,然后单击它,它就像一个符咒,但是当我生成第二个通知时,如果我单击它,应用程序会推两次新页面,第三次推三次,依此类推,即使我通过导航栏返回根页面

有人能帮我理解我做错了什么? 多谢各位

这是根页面的代码

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { Platform } from 'ionic-angular';
import { Page2Page } from '../page2/page2';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {


  constructor(public navCtrl: NavController, private localNotifications: LocalNotifications, public platform: Platform) {

  }

  click(){

      this.platform.ready().then(()=>{

        this.localNotifications.schedule({
          id: 1 ,
          title: 'Notifica',
          text: 'Single ILocalNotification',

        })

        this.localNotifications.on('click').subscribe(()=>{

          console.log("Notification Subscribed")
          this.navCtrl.push(Page2Page);

        })
      })
  }

}
这是第二页(它实际上什么也不做)


每次单击时,您都在订阅一个事件。当您订阅时,您不会覆盖以前的订阅。您需要删除以前的订阅,或者不订阅每次单击。尝试以下方法

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { Platform } from 'ionic-angular';
import { Page2Page } from '../page2/page2';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {


  constructor(public navCtrl: NavController, private localNotifications: LocalNotifications, public platform: Platform) {
        this.localNotifications.on('click').subscribe(()=>{

          console.log("Notification Subscribed")
          this.navCtrl.push(Page2Page);

        })
  }

  click(){

      this.platform.ready().then(()=>{

        this.localNotifications.schedule({
          id: 1 ,
          title: 'Notifica',
          text: 'Single ILocalNotification',

        })


      })
  }

}

我已将localNotifications的订阅移动到构造函数

我解决了这个问题。前面的答案是正确的,我只需要订阅一次,但首先我必须检查平台是否准备好,否则cordova会给我一个错误,应用程序将无法工作

这是密码

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { Platform } from 'ionic-angular';
import { Page2Page } from '../page2/page2';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController,
    private localNotifications: LocalNotifications,
    public platform: Platform) {

      this.platform.ready().then(()=>{

        this.localNotifications.on('click').subscribe(()=>{

        console.log("Notification Subscribed")
        this.navCtrl.push(Page2Page);

        })
      })
  }


  click(){

      this.platform.ready().then(()=>{

        this.localNotifications.schedule({
          id: 1 ,
          title: 'Notifica',
          text: 'Single ILocalNotification',

        })
      })
  }
}

你可以这样做,这是一个魅力的工作

      createLocalNotification(options: any) {
        // Schedule a single notification
        this.localNotifications.schedule(options);
        // Subscribe to onClick event
        let observable = this.localNotifications.on('click').subscribe((data) => {
          this.navCtrl.push(Page2Page);
          // Unsubscribe to OnClick event after push your page
          observable.unsubscribe();
        });
      }

感谢您的回答,但在构造函数中添加订阅无效。。当我在iOS emulator中启动应用程序时,它不会显示任何内容:(我认为你只需要一个if条件,页面将不会被路由..I.e像这样if(this.app.getActiveNav().getViews()[0].name!=“Page2Page”)而不是打开Page2Page,否则什么都不做。通知我这个解决方案是否有效。
      createLocalNotification(options: any) {
        // Schedule a single notification
        this.localNotifications.schedule(options);
        // Subscribe to onClick event
        let observable = this.localNotifications.on('click').subscribe((data) => {
          this.navCtrl.push(Page2Page);
          // Unsubscribe to OnClick event after push your page
          observable.unsubscribe();
        });
      }