Ionic 移动的应用程序中的本地通知

pdkcd3nj  于 2022-12-08  发布在  Ionic
关注(0)|答案(2)|浏览(179)

to give you an overview of what i'm doing, i have a ionic mobile application that uses firebase as its database . One of its features that im implementing is local notifications. Here are my codes
HTML CODE

<button ion-button full (click)="scheduleNotification()">schedule</button>
   <button ion-button full (click)="scheduleNotification2()">schedule</button>

so this is the html code where in there a button allowing you to click or schedule a notification
TYPESCRIPT CODE

import { Component } from '@angular/core';
      import { NavController, Platform} from 'ionic-angular';
      import { LocalNotifications } from '@ionic-native/local-notifications';
    @Component({
    selector: 'page-home',
     templateUrl: 'home.html'
   })
    export class HomePage {

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

     }

    scheduleNotification() {
     this.platform.ready().then(()=>{
      this.localNotif.schedule({
    
        title:'Attention',
        text:'Rk notification',
        at: new Date(new Date().getTime() + 1 * 5000),
       
      });
      });
    }
     
     scheduleNotification2() {
     this.platform.ready().then(()=>{
      this.localNotif.schedule({
    
        title:'Attention',
        text:'Rk notification',
        at: new Date(new Date().getTime() + 1 * 5000),
       
      });
      });
    }

  
}

so the problem here is that, when i click both buttons for the notification to pop out , it only overrides the other. so it only displays one notification. i wanted to display both without overiding. how do i do that?

2admgd59

2admgd591#

你需要传递一个id给你的localNotification,它必须是一个数字和by default it's 0,所以如果你不声明一个id,它将覆盖以前的预定通知。
如果用户能够操作通知(如编辑、删除、更新),我建议将此ID保存在某个地方,以便用户能够这样做。
由于id需要是唯一的,有很多生成id的方法,因为我不知道你的用户多久会创建一次通知,我将使用最简单的生成方法,即Math.random()

scheduleNotification() {
 this.platform.ready().then(()=>{
  this.localNotif.schedule({
    id: Math.round(Math.random() * 1000000)), // this'll generate an id of 7 random digits
    title:'Attention',
    text:'Rk notification',
    at: new Date(new Date().getTime() + 1 * 5000),
  });
  });
}

另一个我可以给予的建议是在尽可能小的时间单位内操纵时间,总是尝试使用毫秒或秒。
希望这能有所帮助。

相关问题