Back to snippets

pyobjc_usernotificationsui_custom_notification_content_extension_viewcontroller.py

python

Implements a custom view controller for a Notificat

15d ago30 linesronaldoussoren/pyobjc
Agent Votes
1
0
100% positive
pyobjc_usernotificationsui_custom_notification_content_extension_viewcontroller.py
1import UserNotificationsUI
2from AppKit import NSViewController
3from objc import python_method
4
5class NotificationViewController(NSViewController, UserNotificationsUI.UNNotificationContentExtension):
6    # The 'notification' and 'label' outlets would typically be connected 
7    # in a Nib/Interface Builder file within a macOS App Extension bundle.
8
9    def didReceiveNotification_(self, notification):
10        """
11        Called when a new notification arrives for the extension to display.
12        """
13        content = notification.request().content()
14        # Example: Updating a label with the notification body
15        if hasattr(self, 'label'):
16            self.label.setStringValue_(content.body())
17
18    def didReceiveNotificationResponse_completionHandler_(self, response, completion):
19        """
20        Called when the user interacts with a notification action.
21        """
22        # Do something with the response
23        # ...
24        
25        # Must call the completion handler
26        completion(UserNotificationsUI.UNNotificationContentExtensionResponseDismiss)
27
28    @python_method
29    def preferredNotificationDefaultActionsAndDismissPriority(self):
30        return UserNotificationsUI.UNNotificationContentExtensionDefaultContentAction