Skip to content

[Windows] Set toast's duration and image #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/local_notifier.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
11 changes: 11 additions & 0 deletions lib/src/local_notification.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<LocalNotificationAction>? actions;

Expand All @@ -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;
Expand All @@ -59,6 +66,8 @@ class LocalNotification with LocalNotificationListener {
body: json['body'],
silent: json['silent'],
actions: actions,
imagePath: json['imagePath'],
duration: json['duration'],
);
}

Expand All @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions lib/src/local_notification_duration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum LocalNotificationDuration {
system,
short,
long,
}
16 changes: 14 additions & 2 deletions windows/local_notifier_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,25 @@ void LocalNotifierPlugin::Notify(
std::get<std::string>(args.at(flutter::EncodableValue("title")));
std::string body =
std::get<std::string>(args.at(flutter::EncodableValue("body")));
std::string imagePath =
std::get<std::string>(args.at(flutter::EncodableValue("imagePath")));
std::string duration =
std::get<std::string>(args.at(flutter::EncodableValue("duration")));

flutter::EncodableList actions = std::get<flutter::EncodableList>(
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 =
Expand Down