通过切换按钮打开/关闭离子设置通知

时间:2019-07-08 07:28:35

标签: angular ionic-framework

我需要知道如何调整开/关通知。我尝试将其存储在本地,就像用户关闭切换一样,将button的值设置为false。如果用户启用切换,则将值设置为true。这种方法可以吗?如果是,那么我尝试IF语句,如果切换按钮的值为true,那么它将显示通知。

但是问题是当按钮值也关闭时,它的显示通知

  constructor(private nativeStorage: NativeStorage, private push: Push, public platform: Platform, private fcm: FCM,  public statusBar: StatusBar, public splashScreen: SplashScreen) {


    this.initializeApp();

  }

  initializeApp() {
       this.platform.ready().then(() => {
       this.check();

      //Notifications
      if(this.isToggled == true){
      this.fcm.subscribeToTopic('all');
      this.fcm.getToken().then(token=>{
          console.log(token);
      })
      this.fcm.onNotification().subscribe(data=>{

        if(data.wasTapped){
                this.nav.setRoot(ArticledetailsPage, {x:data.newsid});

          console.log("Received in background");
        } else {
          console.log("Received in foreground");
        };
      })
      if(this.isToggled == true){
        this.fcm.subscribeToTopic('marketing');
      }

      else{
      this.fcm.unsubscribeFromTopic('marketing');
      }

      //end notifications.

      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.splashScreen.hide();
    });
  }



        notification(){
  this.nav.push(NotificationPage);
  }

public notify() {
  console.log("Toggled: "+ this.isToggled); 
  this.nativeStorage.setItem('toggle', {property: this.isToggled, anotherProperty: 'anotherValue'})
  .then(
    () => console.log('Stored item!'),
    error => console.error('Error storing item', error)
  );

}

check(){
  this.nativeStorage.getItem('toggle')
  .then(
    (data) => {
    console.log(data.property),
    this.isToggled = data.property;
    console.log(this.isToggled);
    }
  );
}
}

1 个答案:

答案 0 :(得分:0)

在您的.html文件中

<h1> {{checked}}</h1>
<ion-toggle color="secondary" (ionChange)="toggleChanged($event)"></ion-toggle>

在您的.ts文件中

  checked:boolean = false;

  constructor() {}

  toggleChanged(eve) {
    console.log(eve.checked);
    this.checked = eve.checked;
    if(!eve.checked) this.fcm.unsubscribeFromTopic('marketing');
    else this.fcm.subscribeToTopic('marketing');
  }
相关问题