Flutter中的闹钟或定时通知

时间:2019-05-20 14:16:00

标签: dart flutter

你好,我是扑朔迷离的新手,我需要发出警报或定时通知。我需要一些类似Android Studio中的AlarmManager。

我要从服务器上花费一个小时,所以我需要在该小时运行警报或通知。如果有人可以帮助我,我将非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用 flutter_local_notifications 在 Flutter 中配置计时器和通知。

这是一个每小时发送一次通知的代码片段。

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

// hourly notification schedule config, set to either `hours: 1` or `minutes: 60`
var scheduledNotificationDateTime =
    DateTime.now().add(Duration(minutes: 60));

Future<void> _showNotificationHourly() async {
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      'your other channel id',
      'your other channel name',
      'your other channel description',),
    );
    const IOSNotificationDetails iOSPlatformChannelSpecifics =
        IOSNotificationDetails();
    const MacOSNotificationDetails macOSPlatformChannelSpecifics =
        MacOSNotificationDetails();
    const NotificationDetails platformChannelSpecifics = NotificationDetails(
        android: androidPlatformChannelSpecifics,
        iOS: iOSPlatformChannelSpecifics,
        macOS: macOSPlatformChannelSpecifics);
    
    // https://pub.dev/documentation/flutter_local_notifications/latest/#scheduling-a-notification
    await flutterLocalNotificationsPlugin.zonedSchedule(
        0,
        'scheduled title',
        'scheduled body',
        scheduledNotificationDateTime,
        platformChannelSpecifics);
}