【问题标题】:Ionic 3 Set notification permission on toggle buttonIonic 3 在切换按钮上设置通知权限
【发布时间】:2019-07-14 20:59:31
【问题描述】:

我需要知道如何在 ionic 应用程序中的切换按钮上使用通知权限。我希望当用户关闭切换时,如果切换打开,用户将无法获得 FCM 推送通知,然后用户可以获得通知。当用户关闭切换时,我尝试使用本地存储,我在 localstoreage 中将切换设置为 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);
    }
  );
}
}

【问题讨论】:

    标签: angular ionic-framework ionic3


    【解决方案1】:

    在您的 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);
            }
          );
      }
    

    【讨论】:

      【解决方案2】:

      正确的做法是编写后端函数来注册注销通知

      Eg : /myapi/stopPushnotification
      

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

      【讨论】:

        【解决方案3】:

        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 level,just update the appConfig in service and use it as refrence when do enable/disable push notifications.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-17
          • 2018-05-08
          • 1970-01-01
          • 2012-10-12
          • 2022-10-14
          • 1970-01-01
          相关资源
          最近更新 更多