likes
comments
collection
share

flutter desktop notifier 轻而易举实现桌面端系統通知

作者站长头像
站长
· 阅读数 23

flutter desktop notifier 轻而易举实现桌面端系統通知

 不管是桌面端还是移动端程序,大多数的程序都具备本地系统通知功能,flutter同样也具备这样的功能,但是需要使用插件,因为flutter是跨平台的,所以本地系统通知在不同平台实现也不同,目前好像没有能在所有平台都实现本地系统通知的插件,所以我们区分桌面端和移动端来实现相关本地系统通知功能,首先实现的是桌面端,使用local_notifier插件。

 完成效果图如下:

flutter desktop notifier 轻而易举实现桌面端系統通知

win_toast插件

 WinToast是一个用C++编写的轻量级库,它带来了Windows 8,Windows 10和Windows 11的现代Toast通知的完整集成。这个插件只针对windows系统,具有丰富的功能,因为只针对windows系统,所以我们暂时不使用它。

 效果图:

flutter desktop notifier 轻而易举实现桌面端系統通知

local_notifier插件

flutter desktop notifier 轻而易举实现桌面端系統通知

 local_notifier插件支持windwos、mac、linux系统的本地系统通知,是一个跨平台插件。我们使用此插件来实现系统通知功能。这个插件好久没有更新了,不知道以后还会不会有人维护。可能是使用的人没那么多吧。

定义全局变量notification

 为了方便使用,我们定义全局变量notification,是一个LocalNotification 类,这样的话,在所有代码中都可以使用这个变量,因为我们只是测试使用,所以这样定义,实际情况中大家自己斟酌使用。我们传入标题"local notifier title",内容"hello flutter!",并添加两个按钮。

LocalNotification notification = LocalNotification(
  title: "local notifier title",
  body: "hello flutter!",
  actions: [
    LocalNotificationAction(text: 'OK'),
    LocalNotificationAction(text: 'Close'),
  ],
);

 效果图:

flutter desktop notifier 轻而易举实现桌面端系統通知  LocalNotification有6个参数:

String? identifier:唯一识别码
required String title:通知的标题
String? subtitle:通知内容的标题
String? body:内容的主体
bool silent = false:在发送通知时是否静音
List<LocalNotificationAction>? actions:通知中显示的按钮

 LocalNotification有5个方法可以使用:

onShow:通知显示时调用
onClose:通知关闭时调用
onClick:通知点击时调用
onClickAction:通知中的按钮被点击时调用
show:显示通知

 不知道为什么,在windwos下传入subtitle参数没有生效,如果有大佬知道请指点迷津。

实例化localNotifier

 我们需要用到一个实例化的 LocalNotifier 对象,调用setup方法进行设置。 appName是应用程序的名称,会在通知中显示。

  WidgetsFlutterBinding.ensureInitialized();
  await localNotifier.setup(
    appName: 'Desktop Notifier',
    // The parameter shortcutPolicy only works on Windows
    shortcutPolicy: ShortcutPolicy.requireCreate,
  );

设置通知发生时的交互

 我们可以设置一些交互动作,监听通知是否被人点击或关闭,进而实现一些交互操作。通过notification.onShow设置通知发生时的一些操作,通过notification.onClick设置通知被点击时进行何种操作。同理可以监听通知关闭和按钮被点击事件,按钮点击事件可以通过索引来判断哪一个按钮被点击。我们这里只是单纯打印相关事件。

  notification.onShow = () {
    print('onShow ${notification.identifier}');
  };
  notification.onClose = (closeReason) {
    // Only supported on windows, other platforms closeReason is always unknown.
    switch (closeReason) {
      case LocalNotificationCloseReason.userCanceled:
        // do something
        break;
      case LocalNotificationCloseReason.timedOut:
        // do something
        break;
      default:
    }
    print('onClose  $closeReason');
  };
  notification.onClick = () {
    print('onClick ${notification.identifier}');
  };
  notification.onClickAction = (actionIndex) {
    print('onClickAction  $actionIndex');
    if (actionIndex == 1) {
      notification.close();
    }
  };

 效果图:

flutter desktop notifier 轻而易举实现桌面端系統通知

notification的方法

 notification有几个方法,我们可以主动对其操作,我们在按钮中调用notification.show(),实现点击按钮发送通知效果。此外还有close()和 destroy()方法,可以对通知进行关闭和销毁。

TextButton(
  onPressed: () {
    notification.show();
  },
  child: const Text("Show Notifier"),
),

 效果图

flutter desktop notifier 轻而易举实现桌面端系統通知

总结

 利用local_notifier基本上可以满足我们的日常使用需求了,上述代码简单的实现了相关功能,希望后续这个插件能继续更新,完善和添加更多功能。

完整代码

import 'package:local_notifier/local_notifier.dart';
LocalNotification notification = LocalNotification(
  title: "local notifier title",
  body: "hello flutter!",
  actions: [
    LocalNotificationAction(text: 'OK'),
    LocalNotificationAction(text: 'Close'),
  ],
);
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await localNotifier.setup(
    appName: 'Desktop Notifier',
    shortcutPolicy: ShortcutPolicy.requireCreate,
  );
  notification.onShow = () {
    print('onShow ${notification.identifier}');
  };
  notification.onClose = (closeReason) {
    switch (closeReason) {
      case LocalNotificationCloseReason.userCanceled:
        break;
      case LocalNotificationCloseReason.timedOut:
        break;
      default:
    }
    print('onClose  $closeReason');
  };
  notification.onClick = () {
    print('onClick ${notification.identifier}');
  };
  notification.onClickAction = (actionIndex) {
    print('onClickAction  $actionIndex');
    if (actionIndex == 1) {
      notification.close();
    }
  };
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const Scaffold(
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [
            children: [
              Lottie.asset(
                'assets/147963-high-five.json',
              ),
            ],
              TextButton(
                onPressed: () {
                  notification.show();
                },
                child: const Text("Show Notifier"),
              ),
        ],
      ),
    );
  }
}
转载自:https://juejin.cn/post/7270028440035442743
评论
请登录