首页 > 解决方案 > 如何在 Flutter 上运行多个通知,每个通知都有指定的时间?

问题描述

我使用每小时重复一次的通知,具有不同的 id 和 channel ,但我只得到了第一个 evert time 和其他被阻止的,有时只有 lastone。

这是我的通知功能

Future<void> _scheduleNotification(id,duration,channelname,channelid,soundandroid,soundios,message,title) async {

   var vibrationPattern = Int64List(4);
   vibrationPattern[0] = 0;
   vibrationPattern[1] = 1000;
   vibrationPattern[2] = 5000;
   vibrationPattern[3] = 2000;
 var androidPlatformChannelSpecifics = AndroidNotificationDetails(
       channelid,
       channelname,
       'your other channel description',
       icon: '@mipmap/ic_launcher',
       importance: Importance.Max,
       priority: Priority.High,
       ongoing: true,
       autoCancel: false,
       sound: RawResourceAndroidNotificationSound(soundandroid),
       largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),
       vibrationPattern: vibrationPattern,
       enableLights: true,
       color: const Color.fromARGB(255, 255, 0, 0),
       ledColor: const Color.fromARGB(255, 255, 0, 0),
       ledOnMs: 1000,
       ledOffMs: 500);
   var iOSPlatformChannelSpecifics =
       IOSNotificationDetails(sound:soundios);
   var platformChannelSpecifics = NotificationDetails(
       androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
   await flutterLocalNotificationsPlugin.periodicallyShow(
       id,
       title,
       message,
       RepeatInterval.Hourly,
       platformChannelSpecifics,
       );
   }

我调用这个函数来执行

 Future<void> smartnotificationcall(channelname,id)
 async{
   if(channelname=="clean"){ Duration(seconds:5); return _scheduleNotification(1,20,'clean',"clean","clean","clean.wav",'test','test');}
   if(channelname=="sport"){ Duration(seconds:20); return _scheduleNotification(2,30,"sport","sport","sport","sport.wav",'test','test');}
   if(channelname=="eat"){ Duration(seconds:40); return _scheduleNotification(3,30,"eat","eat","eat","eat.wav",'test','test');}
   if(channelname=="getout"){ Duration(seconds:50); return _scheduleNotification(4,30,"getout","getout","getout","getout.wav",'test','test');
 } 
}

标签: flutterdartnotificationsdart-pub

解决方案


您使用哪个平台?Android 使用AlarmManager来安排重复通知,它们是不准确的,这是记录在案的 Android 行为。


推荐阅读