Flutter桌面软件开发中实现本地通知

Flutter桌面软件开发中实现本地通知可以使用local_notifier ,local_notifier这个插件允许 Flutter 桌面 应用显示本地通知。

Flutter桌面软件开发中实现本地通知 第一步安装依赖

dependencies:
  local_notifier: ^0.1.5

Flutter桌面软件开发中实现本地通知 第二步配置插件

1、main方法配置

void main() async{
  runApp(const MyApp());
  //配置异步通知
  await localNotifier.setup(
    appName: 'IT营',
    // 参数 shortcutPolicy 仅适用于 Windows
    shortcutPolicy: ShortcutPolicy.requireCreate,
  );
}

2、用到的地方弹窗

LocalNotification notification = LocalNotification(
                    title: "local_notifier_example",
                    body: "hello flutter!",
);

Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  //通知完结

                  LocalNotification notification = LocalNotification(
                    title: "local_notifier_example",
                    body: "hello flutter!",
                  );

                  notification.onShow = () {
                    print('onShow ${notification.identifier}');
                  };
                  notification.onClose = (closeReason) {
                    // 只支持在windows,其他平台 closeReason 始终为 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 ${notification?.identifier} - $actionIndex');
                  };
                  notification.show();
                },
                child: Text("显示notification"))
          ],
        ),
      ),
    );
  }
相关推荐
helloxmg3 小时前
鸿蒙harmonyos next flutter通信之MethodChannel获取设备信息
flutter
helloxmg3 小时前
鸿蒙harmonyos next flutter混合开发之开发package
flutter·华为·harmonyos
lqj_本人1 天前
flutter_鸿蒙next_Dart基础②List
flutter
lqj_本人1 天前
flutter_鸿蒙next_Dart基础①字符串
flutter
The_tuber_sadness1 天前
【Flutter】- 基础语法
flutter
helloxmg1 天前
鸿蒙harmonyos next flutter通信之BasicMessageChannel获取app版本号
flutter
linpengteng2 天前
使用 Flutter 开发数字钱包应用(Dompet App)
前端·flutter·firebase
云兮Coder2 天前
鸿蒙 HarmonyNext 与 Flutter 的异同之处
flutter·华为·harmonyos