From add864caaf303642ab6bd9fab69f439d29dd05e6 Mon Sep 17 00:00:00 2001 From: "n.morozov" Date: Fri, 12 Aug 2022 11:13:29 +0300 Subject: [PATCH] Now is able to pass toast's duration (short is about 5 sec) and image path (when it's null, using Text02, either ImageAndText02) --- lib/local_notifier.dart | 1 + lib/src/local_notification.dart | 11 +++++++++++ lib/src/local_notification_duration.dart | 5 +++++ windows/local_notifier_plugin.cpp | 16 ++++++++++++++-- 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 lib/src/local_notification_duration.dart diff --git a/lib/local_notifier.dart b/lib/local_notifier.dart index 63adf0e..beac5ed 100644 --- a/lib/local_notifier.dart +++ b/lib/local_notifier.dart @@ -1,5 +1,6 @@ export 'src/local_notification_action.dart'; export 'src/local_notification_close_reason.dart'; +export 'src/local_notification_duration.dart'; export 'src/local_notification_listener.dart'; export 'src/local_notification.dart'; export 'src/local_notifier.dart'; diff --git a/lib/src/local_notification.dart b/lib/src/local_notification.dart index ed4dcc0..b263615 100644 --- a/lib/src/local_notification.dart +++ b/lib/src/local_notification.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:local_notifier/src/local_notification_duration.dart'; import 'package:uuid/uuid.dart'; import 'local_notification_action.dart'; @@ -21,6 +22,10 @@ class LocalNotification with LocalNotificationListener { /// Representing whether the notification is silent. bool silent; + LocalNotificationDuration? duration; + + String? imagePath; + /// Representing the actions of the notification. List? actions; @@ -36,6 +41,8 @@ class LocalNotification with LocalNotificationListener { this.body, this.silent = false, this.actions, + this.imagePath, + this.duration, }) { if (identifier != null) { this.identifier = identifier; @@ -59,6 +66,8 @@ class LocalNotification with LocalNotificationListener { body: json['body'], silent: json['silent'], actions: actions, + imagePath: json['imagePath'], + duration: json['duration'], ); } @@ -70,6 +79,8 @@ class LocalNotification with LocalNotificationListener { 'body': body ?? '', 'silent': silent, 'actions': (actions ?? []).map((e) => e.toJson()).toList(), + 'imagePath': imagePath ?? '', + 'duration': duration?.toString().split('.').last ?? '', }..removeWhere((key, value) => value == null); } diff --git a/lib/src/local_notification_duration.dart b/lib/src/local_notification_duration.dart new file mode 100644 index 0000000..c2cef2c --- /dev/null +++ b/lib/src/local_notification_duration.dart @@ -0,0 +1,5 @@ +enum LocalNotificationDuration { + system, + short, + long, +} diff --git a/windows/local_notifier_plugin.cpp b/windows/local_notifier_plugin.cpp index 34ad905..cb00e41 100644 --- a/windows/local_notifier_plugin.cpp +++ b/windows/local_notifier_plugin.cpp @@ -184,13 +184,25 @@ void LocalNotifierPlugin::Notify( std::get(args.at(flutter::EncodableValue("title"))); std::string body = std::get(args.at(flutter::EncodableValue("body"))); + std::string imagePath = + std::get(args.at(flutter::EncodableValue("imagePath"))); + std::string duration = + std::get(args.at(flutter::EncodableValue("duration"))); flutter::EncodableList actions = std::get( args.at(flutter::EncodableValue("actions"))); - - WinToastTemplate toast = WinToastTemplate(WinToastTemplate::Text02); + + WinToastTemplate toast = imagePath.empty() ? WinToastTemplate(WinToastTemplate::Text02) : WinToastTemplate(WinToastTemplate::ImageAndText02); toast.setTextField(converter.from_bytes(title), WinToastTemplate::FirstLine); toast.setTextField(converter.from_bytes(body), WinToastTemplate::SecondLine); + if (!imagePath.empty()) { + toast.setImagePath(converter.from_bytes(imagePath)); + } + if (duration.empty() || duration == "system") { + toast.setDuration(WinToastTemplate::Duration::System); + } else { + toast.setDuration(duration == "long" ? WinToastTemplate::Duration::Long : WinToastTemplate::Duration::Short); + } for (flutter::EncodableValue action_value : actions) { flutter::EncodableMap action_map =