leanflutter / local_notifier

This plugin allows Flutter desktop apps to notify local notifications.
MIT License
71 stars 30 forks source link

[macOS] Notification is not shown if the app is in foreground #12

Closed cbenhagen closed 2 years ago

cbenhagen commented 2 years ago

Often this might be the exepcted behaviour as the notificaiton could also be displayed in your app. But what if the app is only living in the tray using tray_manager or similar?

The behaviour could be configurable in the plugin and maybe even default to always show the alert? Documentation is here.

This would hardcode the behaviour to always show the alert:

diff --git a/macos/Classes/LocalNotifierPlugin.swift b/macos/Classes/LocalNotifierPlugin.swift
index 2ac7d7e..bdfea72 100644
--- a/macos/Classes/LocalNotifierPlugin.swift
+++ b/macos/Classes/LocalNotifierPlugin.swift
@@ -83,6 +83,10 @@ public class LocalNotifierPlugin: NSObject, FlutterPlugin, NSUserNotificationCen
     public func userNotificationCenter(_ center: NSUserNotificationCenter, didDeliver notification: NSUserNotification) {
         _invokeMethod("onLocalNotificationShow", notification.identifier!)
     }
+
+    public func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
+      return true
+    }

     public func _invokeMethod(_ methodName: String, _ notificationId: String) {
         let args: NSDictionary = [
lijy91 commented 2 years ago

I think you can use windowManager.isFocused() to determine if the app is in the foreground.

lijy91 commented 2 years ago
bool isFocused = await windowManager.isFocused();
if (!isFocused) {
  _exampleNotification?.show();
}
cbenhagen commented 2 years ago

Thanks!