Skip to content
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

#14 Implement notification cancellation #15

Merged
merged 3 commits into from
Feb 4, 2024
Merged
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
52 changes: 52 additions & 0 deletions examples/cancel_multiple_notifications.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 3 additions & 3 deletions src/mac_notifications/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down Expand Up @@ -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.
Expand All @@ -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)
13 changes: 12 additions & 1 deletion src/mac_notifications/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cancel_notification(self.uid)
notification_sender.cancel_notification(self.uid)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

clear_notification_from_existence(self.uid)


class NotificationManager(metaclass=Singleton):
"""
The NotificationManager is responsible for managing the notifications. This includes the following:
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion src/mac_notifications/notification_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Loading