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

发布于 1 年前 作者 phonegap100 391 次浏览 最后一次编辑是 1 年前 来自 分享

Flutter桌面软件开发如何实现本地通知ne ? 我们可以使用local_notifier 这个插件允许Flutter 桌面应用显示本地通知。

https://pub.dev/packages/local_notifier

381633085.bmp

安装

dependencies:
  local_notifier: ^0.1.5

用法

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

3、弹出确认框

aa1680506105902.png

 LocalNotification notification = LocalNotification(
                    title: "local_notifier_example",
                    body: "hello flutter!",
                    actions: [
                      LocalNotificationAction(
                        text: 'Yes',
                      ),
                      LocalNotificationAction(
                        text: 'No',
                      ),
                    ],
);
回到顶部