【问题标题】:Does flutter_local_notifications have an onShown listener?flutter_local_notifications 有 onShown 监听器吗?
【发布时间】:2020-07-02 12:31:47
【问题描述】:

由于我可以创建的待处理本地通知的数量有限制,我想在特定时间只设置一个本地通知,当它显示给用户时,设置下一个,依此类推.我找不到任何方法来收听 Flutter 本地通知中的 onShown 事件。有什么解决办法吗?

【问题讨论】:

  • 你应该包含代码示例和对你正在使用的任何库的引用,你的描述太模糊了。

标签: flutter


【解决方案1】:

您可以配置为在用户实际看到当前通知时显示下一个通知。请参阅下面的代码。代码取自flutter_local_notifications 包。

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  @override
  void initState() {
    super.initState();
    ...
    _configureDidReceiveLocalNotificationSubject(); // user is shown alert dialogue
    ...
  }

  @override
  void dispose() {
    didReceiveLocalNotificationSubject.close();
    ...
    super.dispose();
  }


  // This is where the alertDialogue is shown to the user
  void _configureDidReceiveLocalNotificationSubject() {

    didReceiveLocalNotificationSubject.stream
        .listen((ReceivedNotification receivedNotification) async {
      await showDialog(
        ...
      );

      // Now the notification is shown, set the Next Notification
      __scheduleNotification(); 
      // This will be in a cycle, add an if condition to avoid infinite loop of notification.
      // You can pass duration as parameter if needed.
    });

  }


  /// Schedules a notification that specifies a different icon, sound and vibration pattern
  Future<void> _scheduleNotification() async {
    var scheduledNotificationDateTime =
        DateTime.now().add(Duration(seconds: 5));
    var vibrationPattern = Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 1000;
    vibrationPattern[2] = 5000;
    vibrationPattern[3] = 2000;

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description',
        icon: 'secondary_icon',
        sound: 'slow_spring_board',
        largeIcon: 'sample_large_icon',
        largeIconBitmapSource: BitmapSource.Drawable,
        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: 'slow_spring_board.aiff');
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.schedule(
        0,
        'scheduled title',
        'scheduled body',
        scheduledNotificationDateTime,
        platformChannelSpecifics);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...
    );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-10
    • 2012-08-11
    • 2022-08-05
    • 2020-08-30
    • 1970-01-01
    • 2023-03-14
    • 2013-12-24
    • 2014-12-13
    相关资源
    最近更新 更多