diff --git a/examples/cancel_multiple_notifications.py b/examples/cancel_multiple_notifications.py new file mode 100644 index 0000000..333d1e3 --- /dev/null +++ b/examples/cancel_multiple_notifications.py @@ -0,0 +1,52 @@ +import time +from pathlib import Path + +from mac_notifications import client +from mac_notifications.client import Notification + + +if __name__ == "__main__": + print("Sending meeting notification.") + + sent: list[Notification] = [] + + def cancel_all(): + for i in sent: + i.cancel() + + sent.append(client.create_notification( + title="Annoyed by notifications?", + icon=Path(__file__).parent / "img" / "zoom.png", + action_button_str="Cancel all notifications", + action_callback=cancel_all, + )) + + time.sleep(5) + print("Sending notification.") + sent.append(client.create_notification( + title="Message from Henk", + subtitle="Hey Dude, are we still meeting?", + icon=Path(__file__).parent / "img" / "chat.png", + reply_button_str="Reply to this notification", + reply_callback=lambda reply: print(f"You replied to Henk: {reply}"), + )) + + print("Yet another message.") + sent.append(client.create_notification( + title="Message from Daniel", + subtitle="How you doing?", + icon=Path(__file__).parent / "img" / "chat.png", + reply_button_str="Reply to this notification", + reply_callback=lambda reply: print(f"You replied to Daniel: {reply}"), + )) + time.sleep(5) + + for i in sent: + i.cancel() + time.sleep(5) + + print("Application will remain active until both notifications have been answered.") + while client.get_notification_manager().get_active_running_notifications() > 0: + time.sleep(1) + print("all notifications are handled") + client.stop_listening_for_callbacks() diff --git a/src/mac_notifications/client.py b/src/mac_notifications/client.py index 43cb039..0f9fb7a 100644 --- a/src/mac_notifications/client.py +++ b/src/mac_notifications/client.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import Callable -from mac_notifications.manager import NotificationManager +from mac_notifications.manager import Notification, NotificationManager from mac_notifications.notification_config import NotificationConfig """ @@ -36,7 +36,7 @@ def create_notification( reply_button_str: str | None = None, reply_callback: Callable[[str], None] | None = None, snooze_button_str: str | None = None, -) -> None: +) -> Notification : """ Create a MacOS notification :) :param title: Title of the notification. @@ -63,4 +63,4 @@ def create_notification( reply_callback=reply_callback, snooze_button_str=snooze_button_str, ) - get_notification_manager().create_notification(notification_config) + return get_notification_manager().create_notification(notification_config) diff --git a/src/mac_notifications/manager.py b/src/mac_notifications/manager.py index fc229db..5abe003 100644 --- a/src/mac_notifications/manager.py +++ b/src/mac_notifications/manager.py @@ -12,6 +12,7 @@ from mac_notifications.listener_process import NotificationProcess from mac_notifications.notification_config import NotificationConfig from mac_notifications.singleton import Singleton +from mac_notifications.notification_sender import cancel_notification """ This is the module responsible for managing the notifications over time & enabling callbacks to be executed. @@ -30,6 +31,15 @@ logger = logging.getLogger() +class Notification(object): + def __init__(self, uid) -> None: + self.uid = uid + + def cancel(self) -> None: + cancel_notification(self.uid) + clear_notification_from_existence(self.uid) + + class NotificationManager(metaclass=Singleton): """ The NotificationManager is responsible for managing the notifications. This includes the following: @@ -57,7 +67,7 @@ def create_callback_executor_thread(self) -> None: self._callback_executor_event.set() self._callback_executor_thread.start() - def create_notification(self, notification_config: NotificationConfig) -> None: + def create_notification(self, notification_config: NotificationConfig) -> Notification: """ Create a notification and the corresponding processes if required for a notification with callbacks. :param notification_config: The configuration for the notification. @@ -78,6 +88,7 @@ def create_notification(self, notification_config: NotificationConfig) -> None: _FIFO_LIST.append(notification_config.uid) _NOTIFICATION_MAP[notification_config.uid] = notification_config self.clear_old_notifications() + return Notification(notification_config.uid) @staticmethod def clear_old_notifications() -> None: diff --git a/src/mac_notifications/notification_sender.py b/src/mac_notifications/notification_sender.py index 7081991..f4a045b 100644 --- a/src/mac_notifications/notification_sender.py +++ b/src/mac_notifications/notification_sender.py @@ -16,7 +16,6 @@ This module is responsible for creating the notifications in the C-layer and listening/reporting about user activity. """ - def create_notification(config: JSONNotificationConfig, queue_to_submit_events_to: SimpleQueue | None) -> Any: """ Create a notification and possibly listed & report about notification activity. @@ -107,3 +106,9 @@ def userNotificationCenter_didActivateNotification_( # return notification return new_notif + + +def cancel_notification(uid:str) -> None: + notification = NSUserNotification.alloc().init() + notification.setIdentifier_(uid) + NSUserNotificationCenter.defaultUserNotificationCenter().removeDeliveredNotification_(notification)