Ionic 2:多次执行点击事件的本地通知

时间:2017-06-07 17:18:10

标签: cordova notifications ionic2 cordova-plugins localnotification

我尝试在Ionic 2中实现本地通知。我按照文档中提到的每个步骤进行操作。而且它也在工作。但是我在click事件中遇到了问题。

我在here中也发现了同样类型的问题。但解决方案是Ionic 1。

我的问题是这样的:

当我安排任何提醒/本地通知时,它会被注册。当我继续添加提醒时,它也会被添加。但是当谈到获得通知时,我正面临着这个问题。当我点击第一个通知时,我得到了所需的输出。但是当我安排下一个通知并点击它时。我得到输出两次。如果我设置了多个通知,就会发生同样的事情。即,如果我安排两个或两个以上的通知,我会在点击通知事件中获得相同数量的警报消息。我使用的示例代码在这里:

构造

notifications:any;

 constructor(public navCtrl: NavController,
                  public alertCtrl: AlertController,
                  private localNotifications: LocalNotifications,
                  public platform: Platform,
                  public loadingCtrl: LoadingController,
                  public navParams: NavParams) {

          this.platform.ready().then((readySource) => {
           this.localNotifications.on('click', (notification, state) =>{

                  let eventData = JSON.parse(notification.data);

                  let alert = this.alertCtrl.create({
                    title: 'Reminder',
                    message: 'You have a notification of  '+eventData.data+'. Please click below to see details. ',
                    buttons: [
                      {
                        text: 'OK',
                        handler: () => {
                          this.cancelReminder(notification.id);
                        }
                      }
                    ]
                  });
                  alert.present();
                });
        });
      }

添加提醒和其他方法:

addReminder(reminderId, displayMessage) {

    let loader = this.loadingCtrl.create({
      content: "Setting up Reminder..."
    });
    loader.present();

    this.localNotifications.getAllScheduled().then(data=> {
      this.notifications = data;
      let notification = {
        id: reminderId,
        title: 'Reminder Notification',
        text: 'Notification Head',
        at: new Date(new Date().getTime() + 60 * 1000),
        data: {
          data: displayMessage
        }
      }
      this.notifications.push(notification);
      this.localNotifications.clearAll().then(clearData => {
        loader.dismiss().catch(() => {
        });
        this.localNotifications.schedule(this.notifications);

      });
    });

    }

    cancelReminder(eventId) {
    this.localNotifications.cancel(eventId).then( ()=> {
      this.navCtrl.setRoot(Page);
    });
  }

代码中有错误吗?或者是否有另一种方法来实现它?

0 个答案:

没有答案
相关问题