User Notifications UI Framework (PyObjC)

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and Objective-C, allowing Python scripts to use and extend existing Objective-C class libraries, including Cocoa frameworks on macOS. The `pyobjc-framework-usernotificationsui` package provides Python wrappers for the macOS UserNotificationsUI framework. This framework enables developers to customize the appearance and interactivity of local and remote notifications by adding a notification content app extension to their macOS applications. The current version is 12.1, and PyObjC generally follows an annual release cadence, typically in October.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic structure for a Python class using `pyobjc-framework-usernotificationsui` to define a custom notification content extension. This framework is specifically for customizing the *appearance* and *interactivity* of notifications within a dedicated app extension, not for sending notifications. The code must be integrated into a macOS app extension project, typically set up via Xcode, where this Python class would act as the `UNNotificationContentExtension`'s view controller. It cannot be run as a standalone script.

import objc
from AppKit import NSViewController
from Foundation import NSObject, NSLog, UNNotification
from UserNotificationsUI import UNNotificationContentExtension

class MyNotificationViewController (NSObject, NSViewController, UNNotificationContentExtension):
    """
    This class defines a custom view controller for a rich notification.
    It runs as part of a macOS Notification Content Extension.
    This code is illustrative and not directly runnable as a standalone script.
    It requires an Xcode project with a Notification Content Extension target
    configured to use this Python class as its principal class.
    """

    # @objc.IBOutlet('myLabel') # Example if using a NIB/Storyboard
    # myLabel = objc.ivar.IBOutlet()

    def viewDidLoad(self):
        super().viewDidLoad()
        NSLog("MyNotificationViewController: viewDidLoad called!")
        # Access your UI elements here, if loaded from NIB/Storyboard
        # if self.myLabel:
        #    self.myLabel.setStringValue_("Hello from PyObjC notification!")

    def didReceiveNotification_(self, notification: UNNotification):
        """
        Called when a new notification arrives for the extension to display.
        """
        content = notification.request().content()
        NSLog(f"Received notification with title: {content.title()}, body: {content.body()}")
        # Update your custom UI elements based on the notification content
        # if self.myLabel:
        #    self.myLabel.setStringValue_(content.body())

    def didReceiveNotificationResponse_completionHandler_(self, response, completionHandler):
        """
        Called when the user interacts with your custom notification UI (e.g., clicks a button).
        """
        NSLog(f"Received response for action: {response.actionIdentifier()} with text: {response.userText()}")
        # Perform actions based on user interaction
        completionHandler(UNNotificationContentExtension.UNNotificationContentExtensionResponseOptionDoNotDismiss)

# To integrate this, you would typically build a macOS application with py2app
# and include a Notification Content Extension target in Xcode that points to
# this Python code as its principal class.

view raw JSON →