Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Generate Unique Integer ID Local Notification

Tags:

flutter

enter image description here

I have case when i should show 2 different schedule notification when user press button. But the problem is both of notification have same ID , it will make old notification will replace with new notification. So i make another approach to generating unique ID using DateTime().now.millisecondSinceEpoch.
Another problem with this approach is , i got error 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]:.
How i create unique INTEGER id for different notification ?

1599943560000 is my convert from DateTime to millisecondSinceEpoch

Notification source code


  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,
    );
  }
like image 686
Zeffry Reynando Avatar asked May 24 '26 20:05

Zeffry Reynando


1 Answers

You are better off getting 2 random numbers. Use the Random class for this.

Like this:

  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);
like image 92
Didier Prophete Avatar answered May 27 '26 09:05

Didier Prophete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!