为了让您大致了解我在做什么,我有一个使用 firebase 作为其数据库的 ionic 移动应用程序。我实现的其中一项功能是本地通知。这是我的代码
代码
<button ion-button full (click)="scheduleNotification()">schedule</button>
<button ion-button full (click)="scheduleNotification2()">schedule</button>
所以这是 html 代码,其中有一个按钮允许您单击或安排通知
打字代码
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'
}) 导出类主页{
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),
});
});
}
}
所以这里的问题是,当我单击两个按钮以弹出通知时,它只会覆盖另一个。所以它只显示一个通知。我想在不覆盖的情况下显示两者。我怎么做?
您需要将 an 传递id
给您的localNotification
,它必须是一个数字,默认情况下它是 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),
});
});
}
我可以给出的另一个建议是在尽可能小的时间单位内操纵时间,始终尝试使用毫秒或秒。
希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句