【问题标题】:How to schedule a single local notification multiple times in Flutter如何在 Flutter 中多次安排单个本地通知
【发布时间】:2020-11-17 06:57:36
【问题描述】:

我一直在从事 Flutter 项目,现在我正在尝试使用 flutter_local_notification 安排通知。我已经将该依赖项集成到我的项目中,并且工作正常。 现在我想安排一个通知在应用程序上提醒,比如 2 天前,4 小时前,30 分钟前。

例如,我的应用程序有一个名为 A 的事件,然后我想安排该事件 A 在事件 A 开始前 1 天、1 小时前和 30 分钟前向用户发送通知.所以它会发送 3 次通知。

对于我的场景:为了发送通知,我们必须传递通知 ID。让我们考虑事件 A 的 id 为“123”。我是否必须使用相同的通知 ID“123”安排该通知 3 次?

让我们检查一下我为发送通知而编写的代码:

Future<void> showNotification({
  @required int id,
  @required DateTime dateTime,
  String title,
  String body,
}) async {
  final TimeZone timeZone = TimeZone();
  final String tzName = await timeZone.getTimeZoneName();
  final tz.Location location = await timeZone.getLocation(tzName);
  final scheduledDate = tz.TZDateTime(
    location,
    dateTime.year,
    dateTime.month,
    dateTime.day,
    dateTime.hour,
    dateTime.minute,
    dateTime.second,
  );
  log('------------> Schedule date $scheduledDate');
  final AndroidNotificationDetails androidChannelSpecifics =
     AndroidNotificationDetails(
    '$id',
   'NAME $id',
   'DESCRIPTION $id',
   importance: Importance.max,
   priority: Priority.high,
   ledOnMs: 1000,
   ledOffMs: 500,
 );
 final IOSNotificationDetails iosNotificationSpecifics =
     IOSNotificationDetails();
 final notificationDetails = NotificationDetails(
     iOS: iosNotificationSpecifics, android: androidChannelSpecifics);
 await flutterLocalNotificationsPlugin.zonedSchedule(
  id,
  title,
  body,
  scheduledDate,
  notificationDetails,
  matchDateTimeComponents: DateTimeComponents.time,
  uiLocalNotificationDateInterpretation: null,
  androidAllowWhileIdle: true,
 );
}

通知初始化,比如AndroidManifest.xml中的配置,我也已经做了。

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
final BehaviorSubject<ReceivedNotification> didReceivedNotificationSub =
    BehaviorSubject<ReceivedNotification>();
InitializationSettings initializationSettings;
LocalNotificationiHelper._() {
  init();
}

Future<void> init() async {
  flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  tz.initializeTimeZones();
  if (Platform.isIOS) {
   _requestIOSPermission();
  }
  initialPlatformSpecifics();
}

void initialPlatformSpecifics() {
  const AndroidInitializationSettings initializeAndroidSetting =
      AndroidInitializationSettings('@mipmap/ic_launcher');
  final IOSInitializationSettings initializeIOSSetting =
      IOSInitializationSettings(
       requestAlertPermission: true,
       requestBadgePermission: true,
      requestSoundPermission: false,
     onDidReceiveLocalNotification:
      (int id, String title, String body, String payload) async {
      final ReceivedNotification receivedNotification = ReceivedNotification(
         id: id, title: title, body: body, payload: payload);
         didReceivedNotificationSub.add(receivedNotification);
      },
   );
   initializationSettings = InitializationSettings(
      android: initializeAndroidSetting, iOS: initializeIOSSetting);
}

void _requestIOSPermission() {
   flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
         IOSFlutterLocalNotificationsPlugin>()
     .requestPermissions(
      alert: false,
      badge: true,
      sound: true,
    );
 }

您可能已经注意到 TimeZone() 类,它只是我创建的另一个类,用于获取用户的当前时区。

这是TimeZone()的类

class TimeZone {
   factory TimeZone() => _this ?? TimeZone._();

   TimeZone._() {
     initializeTimeZones();
   }
   static TimeZone _this;

  Future<String> getTimeZoneName() async =>
     FlutterNativeTimezone.getLocalTimezone();

  Future<t.Location> getLocation([String timeZoneName]) async {
     if (timeZoneName == null || timeZoneName.isEmpty) {
       timeZoneName = await getTimeZoneName();
     }
   return t.getLocation(timeZoneName);
  }
}

例如,ID 为 123 的事件 A 将从 2020-11-19 16:00:00 开始。 所以我想知道,我们是否应该安排 3 次通知,以便在活动开始前 1 天、3 小时和 30 分钟前发送??

【问题讨论】:

标签: android flutter dart uilocalnotification


【解决方案1】:

这取决于您希望如何设置间隔。使用flutter_local_notifications 安排定期通知的一种方法是使用flutterLocalNotificationsPlugin.periodicallyShow(),您可以在其中设置RepeatInterval.everyMinute 之类的间隔 - 用法可以在docs 中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多