From fea3eed953da8c4bbff789fe70259594abd980a4 Mon Sep 17 00:00:00 2001 From: Altafur Rahman Date: Fri, 23 Aug 2024 03:33:24 +0600 Subject: [PATCH] added notification feature for desktop --- lib/src/pages/sizzle/sizzle_home.dart | 82 ++++++++++++------- linux/flutter/generated_plugin_registrant.cc | 4 + linux/flutter/generated_plugins.cmake | 1 + macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 + windows/flutter/generated_plugins.cmake | 1 + 7 files changed, 66 insertions(+), 28 deletions(-) diff --git a/lib/src/pages/sizzle/sizzle_home.dart b/lib/src/pages/sizzle/sizzle_home.dart index 9ddd606393..acfe75c8ac 100644 --- a/lib/src/pages/sizzle/sizzle_home.dart +++ b/lib/src/pages/sizzle/sizzle_home.dart @@ -5,6 +5,7 @@ import 'package:blt/src/pages/sizzle/sizzle_state_provider.dart'; import 'package:blt/src/pages/sizzle/sizzle_timer.dart'; import 'dart:async'; import 'package:awesome_notifications/awesome_notifications.dart'; //Added for awesome notifications +import 'package:local_notifier/local_notifier.dart'; class SizzleHome extends ConsumerStatefulWidget { const SizzleHome({Key? key}) : super(key: key); @@ -47,39 +48,64 @@ class _SizzleHomeState extends ConsumerState } void _initializeNotifications() { - AwesomeNotifications().initialize( - null, //Notification icon - [ - NotificationChannel( + if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) { + _initializeDesktopNotifications(); + } else { + AwesomeNotifications().initialize( + null, //Notification icon + [ + NotificationChannel( + channelKey: 'sizzle_timer_channel', + channelName: 'Sizzle Timer Notifications', + channelDescription: + 'Notifications for Sizzle Timer', //Added channel description + defaultColor: Color(0xFFFD5D00), + importance: NotificationImportance.High, + channelShowBadge: true, + ) + ], + channelGroups: [ + NotificationChannelGroup( + channelGroupKey: 'sizzle_timer_group', + channelGroupName: 'sizzle Timer Group', //Added channel group name + ) + ], + debug: true, + ); + } + } + + Future _showNotification() async { + if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) { + await _showDesktopNotification(); + } else { + AwesomeNotifications().createNotification( + content: NotificationContent( + id: 14, channelKey: 'sizzle_timer_channel', - channelName: 'Sizzle Timer Notifications', - channelDescription: - 'Notifications for Sizzle Timer', //Added channel description - defaultColor: Color(0xFFFD5D00), - importance: NotificationImportance.High, - channelShowBadge: true, - ) - ], - channelGroups: [ - NotificationChannelGroup( - channelGroupKey: 'sizzle_timer_group', - channelGroupName: 'sizzle Timer Group', //Added channel group name - ) - ], - debug: true, + title: 'Timer Stopped', + body: 'The timer has stopped. Are you still working?', + notificationLayout: NotificationLayout.Default, + ), + ); + } + } + + void _initializeDesktopNotifications() async { + await localNotifier.setup( + appName: 'Sizzle Timer', // Set your app name + shortcutPolicy: + ShortcutPolicy.requireCreate, // Relevant mainly for Windows ); } - Future _showNotification() async { - AwesomeNotifications().createNotification( - content: NotificationContent( - id: 14, - channelKey: 'sizzle_timer_channel', - title: 'Timer Stopped', - body: 'The timer has stopped. Are you still working?', - notificationLayout: NotificationLayout.Default, - ), + Future _showDesktopNotification() async { + LocalNotification notification = LocalNotification( + title: 'Timer Stopped', + body: 'The timer has stopped. Are you still working?', ); + + await notification.show(); } @override diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index b1deb548da..e26202039c 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,9 @@ void fl_register_plugins(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); + g_autoptr(FlPluginRegistrar) local_notifier_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "LocalNotifierPlugin"); + local_notifier_plugin_register_with_registrar(local_notifier_registrar); g_autoptr(FlPluginRegistrar) pasteboard_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "PasteboardPlugin"); pasteboard_plugin_register_with_registrar(pasteboard_registrar); diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index fdbbeffd82..7a1cb44514 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST awesome_notifications file_selector_linux flutter_secure_storage_linux + local_notifier pasteboard sentry_flutter url_launcher_linux diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 920f46b0d6..a443e6532c 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -8,6 +8,7 @@ import Foundation import awesome_notifications import file_selector_macos import flutter_secure_storage_macos +import local_notifier import package_info_plus import pasteboard import path_provider_foundation @@ -20,6 +21,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AwesomeNotificationsPlugin.register(with: registry.registrar(forPlugin: "AwesomeNotificationsPlugin")) FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) + LocalNotifierPlugin.register(with: registry.registrar(forPlugin: "LocalNotifierPlugin")) FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin")) PasteboardPlugin.register(with: registry.registrar(forPlugin: "PasteboardPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) diff --git a/pubspec.yaml b/pubspec.yaml index f7ad968020..d9ad6d8f48 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: webview_flutter: ^3.0.4 permission_handler: ^11.3.1 awesome_notifications: ^0.9.3+1 + local_notifier: ^0.1.6 dev_dependencies: flutter_launcher_icons: ">=0.9.0 <0.13.0" diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 5e0957c5f5..d8141fe1cc 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { registry->GetRegistrarForPlugin("FileSelectorWindows")); FlutterSecureStorageWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); + LocalNotifierPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("LocalNotifierPlugin")); PasteboardPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("PasteboardPlugin")); PermissionHandlerWindowsPluginRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index ac6c9404ec..34ff9cd03c 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST awesome_notifications file_selector_windows flutter_secure_storage_windows + local_notifier pasteboard permission_handler_windows sentry_flutter