Ionic 3在切换按钮上设置通知权限

时间:2019-07-01 14:24:01

标签: angular ionic-framework ionic3

我需要知道如何在切换按钮上的ionic应用程序中使用通知权限。我想要当用户关闭切换开关时,如果打开切换开关,则用户将无法获得FCM推送通知,则用户可以收到通知。当用户关闭切换时,我尝试使用本地存储。我在本地存储中设置了切换false,因此当用户再次打开应用时,切换按钮处于关闭状态。

  <ion-item>
    <ion-toggle [(ngModel)]="isToggled" (ionChange)="notify()" item-start checked="true" ></ion-toggle>
        <ion-label item-end style="text-align: right;">تلقي الاشعارات
</ion-label>
  </ion-item>

.ts

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

3 个答案:

答案 0 :(得分:2)

很简单,在notify()函数中,如果打开的应用程序的值为true则this.fcm.subscribeToTopic('all');,则打开的应用程序的用户值为false或true。如果它是假的,那就退订它。希望这是您的完美答案

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

      //Notifications
      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");
        };
      })


      //end notifications.

      if (this.isToggled == true) {
        this.fcm.subscribeToTopic('all');
      }

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

      // 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)
      );
    if (this.isToggled == true) {
      this.fcm.unsubscribeFromTopic('all');
    }
    else {
      this.fcm.unsubscribeFromTopic('all');
    }
  }

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

答案 1 :(得分:0)

正确的方法是通过写后端功能注册注销通知

Eg : /myapi/stopPushnotification

该令牌从我们的数据库中删除/添加令牌或将其跟踪到另一个表。在应用程序上,我们具有切换按钮以将其打开和关闭。这是最好的方法。

答案 2 :(得分:0)

1)创建服务

notification service.ts
====================
appConfig = {
 pushNotificationStatus : true    // false

}

2)进行服务调用以启用/禁用切换按钮上的pushNotificationStatus并将notification service.ts appconfig中的值更新为:-

// suppose putservice call for enable/disable push notification

constructor(public notifServ: NotificationService){}
ontoggle(){
 this.https.post(url,{}).subscribe(res =>{
  this.notifServ.appConfig.pushNotificationStatus = true // false based on service call
})
}

3)app.component.ts or component load,检查是否启用了pushNotification    适用于某些呼叫中的应用(可以是个人档案呼叫或任何发送pushNotification状态的呼叫    用户的应用)   a)在

中调用并更新值
this.notifServ.appConfig.pushNotificationStatus = true // false

或者,

在localStorage中执行此操作不是最好的方法,但是如果要在客户端级别处理它,可以这样做:-

For this, whenever your component loads `clear localStorage` or set `push notiifcation key` in localStorage as empty or null or may delete.

注意:上述第一种方法,即service also works at client leveljust update the appConfig in service and use it as refrence when do enable/disable push notifications.