飘扬的本地通知未取消

发布于 2025-02-13 07:54:02 字数 1304 浏览 0 评论 0原文

我的通知API代码:

  static final _notificationPlugin = FlutterLocalNotificationsPlugin();

  static Future<void> showShNotification() async {
    tz.initializeTimeZones();

    _notificationPlugin.zonedSchedule(
        4,
        'Some_Title',
        'Some_Body',
        TZDateTime.from(DateTime.now().add(const Duration(seconds:5)),
            getLocation('Asia/Kolkata')),
        await _notificationDetails(),payload: 'some_payload',
        uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
        androidAllowWhileIdle: true,);
  }

  static Future _notificationDetails() async {
    return NotificationDetails(
      // All relevant notification details here
    ));
  }

  static Future<void> done(int id) async {
   await _notificationPlugin.cancel(id);
  }

在TextButton Widget中:

onPressed: () async {

 NotificationApi.showShNotification();
 await NotificationApi.done(4); // for cancellation

},

带有ID的本地通知(此处为4)未取消。它在5秒后出现。如果我使用了FlutterLocalnotificationplugin()的cancelall()函数,则仅将其取消。是一个错误还是我做错了什么? 请帮助。

编辑:我找到了一个可能的解决方案:

 await NotificationApi.showShNotification();
 await NotificationApi.done(4);

不是100%确定。但是我猜这是由于插件有点迟钝的事实。

My Notification API Code:

  static final _notificationPlugin = FlutterLocalNotificationsPlugin();

  static Future<void> showShNotification() async {
    tz.initializeTimeZones();

    _notificationPlugin.zonedSchedule(
        4,
        'Some_Title',
        'Some_Body',
        TZDateTime.from(DateTime.now().add(const Duration(seconds:5)),
            getLocation('Asia/Kolkata')),
        await _notificationDetails(),payload: 'some_payload',
        uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
        androidAllowWhileIdle: true,);
  }

  static Future _notificationDetails() async {
    return NotificationDetails(
      // All relevant notification details here
    ));
  }

  static Future<void> done(int id) async {
   await _notificationPlugin.cancel(id);
  }

In TextButton Widget :

onPressed: () async {

 NotificationApi.showShNotification();
 await NotificationApi.done(4); // for cancellation

},

Local Notification with the given id(here 4) is not cancelled. It appears after 5 seconds. If I used cancelAll() function of FlutterLocalNotificationsPlugin(), only then it is cancelled. Is it a bug or I am doing something wrong?
Kindly help.

EDIT: I found a possible solution to this :

 await NotificationApi.showShNotification();
 await NotificationApi.done(4);

Not 100% sure. But I am guessing this is due to the fact that the plugin is a bit sluggish.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文