本地定时通知只通知一个定时通知

  • 本文关键字:通知 定时 一个 flutter dart
  • 更新时间 :
  • 英文 :


我跟随了一个关于如何安排本地通知的youtube视频,因为我想制作一个祈祷时间应用程序,每次祈祷时间到达时都会发送通知。我用adhan。Dart是用来检查祈祷时间的包。然而,通知只适用于isyak祈祷时间,即使我安排了5次祈祷时间。

class Notifications {

initialize(BuildContext context) async {
void selectNotification(NotificationResponse notificationResponse) async {
final String? payload = notificationResponse.payload;
if (notificationResponse.payload != null) {
debugPrint('notification payload: $payload');
}
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => bottomnavbar()),
);
}
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: selectNotification);
}
setNotification({required DateTime time ,required  int id,required String wkto, required String? azan}) async {
tz.initializeTimeZones();
Duration duration = time.difference(DateTime.now());
if(!duration.isNegative) {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'Waktu Solat $wkto telah masuk ',
'Ayuh Tunaikan Solat Sekarang',
tz.TZDateTime.now(tz.local).add(duration),
const NotificationDetails(
android: AndroidNotificationDetails(
'App',
'Poket Solat App',
channelDescription: 'Azan untuk Solat',
priority: Priority.max,
importance: Importance.high,
playSound: true,
)),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
}
cancelNotification(){
flutterLocalNotificationsPlugin.cancelAll();
}
}

通知包装器安排祈祷时间通知

class AzanWaktu{
final bool isAzanOn;
final String azanName;
const AzanWaktu({
this.azanName = "Omar & Hana  Azan  Istimewa Ramadan.mp3", this.isAzanOn = true});
}

class AzanReminders{
AzanWaktu azanSubuh;
AzanWaktu azanZohor;
AzanWaktu azanAsar;
AzanWaktu azanMaghrib;
AzanWaktu azanIsyak;
AzanReminders({
this.azanSubuh = const AzanWaktu(),
this.azanZohor = const AzanWaktu(),
this.azanAsar = const AzanWaktu(),
this.azanMaghrib =const AzanWaktu(),
this.azanIsyak  = const AzanWaktu()});
List<AzanReminder> azanReminders=[];
initialization(){
for(var i = 0; i<30; i++){
// DateTime dateTime = DateTime.now().add(Duration(days:i));
if(azanSubuh.isAzanOn){
azanReminders.add(AzanReminder(
id: 1 + 5 * i,azan:azanSubuh.azanName,time: prayerTimes.fajr,wkto:'Subuh'
));
}
if(azanZohor.isAzanOn){
azanReminders.add(AzanReminder(
id: 2 + 5 * i,azan:azanZohor.azanName,time: prayerTimes.dhuhr,wkto:'Zohor'
));
}
if(azanAsar.isAzanOn){
azanReminders.add(AzanReminder(
id: 3 + 5 * i,azan:azanAsar.azanName,time: prayerTimes.asr,wkto:'Asar'
));
}
if(azanMaghrib.isAzanOn){
azanReminders.add(AzanReminder(
id: 4 + 5 * i,azan:azanMaghrib.azanName,time: prayerTimes.maghrib,wkto:'Maghrib'
));
}
if(azanIsyak.isAzanOn){
azanReminders.add(AzanReminder(
id: 5 + 5 * i,azan:azanIsyak.azanName,time: prayerTimes.isha,wkto:'Isyak'
));
}
}
}
}

class AzanReminder{
int id;
String azan;
String wkto;
DateTime time;
AzanReminder({required this.id,required this.azan,required this.wkto,required this.time});
}
初始化通知的函数
void initState(){
super.initState();
isAzanGenerated = false;
if (!isAzanGenerated) {
AzanReminders azanReminders = AzanReminders(
azanSubuh: AzanWaktu(isAzanOn: true),
azanZohor: AzanWaktu(isAzanOn: true),
azanAsar: AzanWaktu(
isAzanOn: true, azanName: ""),
azanMaghrib: AzanWaktu(
isAzanOn: true, azanName: ""),
);
azanReminders.initialization();
Notifications notifications = Notifications();
notifications.initialize(context);
notifications.cancelNotification();
for (var azanReminder in azanReminders.azanReminders) {
notifications.setNotification(
time: azanReminder.time,
id: azanReminder.id,
wkto: azanReminder.wkto,
azan: azanReminder.azan);
isAzanGenerated = true;
}
}
}

祝福我的兄弟,

flutterLocalNotificationsPlugin.zonedSchedule方法中,你应该为每个通知使用唯一的id,如果你用相同的id调度5个通知,那么只有最后一个会被发送。

我建议你使用这样的唯一id的方法:(这就是我所做的)

String getPrayerNotificationId(DateTime time) {
return DateFormat('yyyyMMddkkmm').format(time);
}

希望还是有意义的

我也遇到过同样的问题,但是当我查看区域时间表函数时,有一个参数"matchDateTimeComponents"默认情况下它设置为

matchDateTimeComponents: DateTimeComponents.time

改为

matchDateTimeComponents: DateTimeComponents.dateAndTime

最新更新