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

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

Flutter桌面软件开发中实现本地通知_第1张图片

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"))
          ],
        ),
      ),
    );
  }

你可能感兴趣的:(flutter)