Flutter:生成唯一整数 ID 本地通知

Zef*_*ndo 8 flutter

在此输入图像描述

我有这样的情况,当用户按下按钮时我应该显示 2 个不同的时间表通知。但问题是两个通知具有相同的 ID,这将使旧通知将替换为新通知。因此,我采用另一种方法来使用DateTime().now.millisecondSinceEpoch生成唯一 ID 。
这种方法的另一个问题是,我收到错误 max length INTEGER Unhandled Exception: Invalid argument (id): must fit within the size of a 32-bit integer i.e. in the range [-2^31, 2^31 - 1]:
如何为不同的通知创建唯一的INTEGER id?

1599943560000 是我从 DateTime 到 millisecondSinceEpoch 的转换

通知源代码


  Future<void> scheduleNotification({
    @required DateTime dateTimeShowNotification,
    @required int idNotification,
    String titleNotification = 'default title',
    String bodyNotification = 'default body',
    String payloadNotification = 'payload schedule notification',
  }) async {
    var androidChannelSpecifics = AndroidNotificationDetails(
      'CHANNEL_ID 2',
      'CHANNEL_NAME 2',
      'CHANNEL_DESCRIPTION 2',
      sound: RawResourceAndroidNotificationSound(soundName),
      largeIcon: DrawableResourceAndroidBitmap(largeIcon),
      enableLights: true,
      color: const Color.fromARGB(255, 255, 0, 0),
      ledColor: const Color.fromARGB(255, 255, 0, 0),
      ledOnMs: 1000,
      ledOffMs: 500,
      importance: Importance.Max,
      priority: Priority.High,
      playSound: true,
      timeoutAfter: 5000,
      autoCancel: true,
      styleInformation: DefaultStyleInformation(true, true),
    );
    var iosChannelSpecifics = IOSNotificationDetails(
      sound: '$soundName.aiff',
    );
    var platformChannelSpecifics = NotificationDetails(
      androidChannelSpecifics,
      iosChannelSpecifics,
    );
    await flutterLocalNotificationsPlugin.schedule(
      idNotification,
      titleNotification,
      bodyNotification,
      dateTimeShowNotification,
      platformChannelSpecifics,
      payload: payloadNotification,
    );
  }
Run Code Online (Sandbox Code Playgroud)

Did*_*ete 4

你最好获得 2 个随机数。Random为此使用该类。

像这样:

  var random = Random(); // keep this somewhere in a static variable. Just make sure to initialize only once.
  int id1 = random.nextInt(pow(2, 31) - 1);
  int id2 = random.nextInt(pow(2, 31) - 1);
Run Code Online (Sandbox Code Playgroud)