【问题标题】:How can I run multiple notifications on flutter with specified time for each one?如何在 Flutter 上运行多个通知,每个通知都有指定的时间?
【发布时间】:2020-04-13 18:26:11
【问题描述】:

我使用每小时重复一次的通知,具有不同的 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');
 } 
}

【问题讨论】:

    标签: flutter dart notifications dart-pub


    【解决方案1】:

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

    【讨论】:

    • 你好,谢谢回复,我用flutter,这个代码在ios和android都可以用,flutter中也有alarm_manager,不过只能在android上用。
    • 我在颤振中使用相同的通知插件,如果你想收到确切的通知,你应该使用 .scheduleNotification 并且当第一个通知触发时,安排下一个。没有办法为 Android 安排精确的重复通知,对于 iOS,它可以正常工作
    • 再次感谢,我想我已经找到了一种方法,使用 reapet Interval 函数,我之前尝试过 shedled 但它并没有完全按照我想要的方式工作,我也会按照你说的方式进行测试我不知道如果我调用它是否会再次执行函数代码,但如果我在其中使用相同的函数想象会发生什么,它将产生大量的停止机器的工作
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多