flutter 计划的本地通知仅通知一个计划的通知

nzrxty8p  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(193)

我关注了一个关于如何安排本地通知的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();
  }
}

通知 Package 器来安排祷告时间通知

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;
      }
    }
  }
70gysomp

70gysomp1#

我的兄弟,
在方法flutterLocalNotificationsPlugin.zonedSchedule中,您应该为每个通知使用唯一的ID,如果您计划使用相同的ID发送5个通知,则只会发送最后一个通知。
我建议你使用这样的方法来获得一个唯一的id:(我就是这么做的)

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

希望还能有用。

相关问题