PyObjC NotificationCenter Framework
PyObjC-framework-NotificationCenter provides Python bindings for Apple's NotificationCenter.framework on macOS, allowing Python applications to interact with the system's user notification services. It primarily wraps the older NSUserNotification API. As part of the larger PyObjC project, it maintains an active development pace, with releases often coinciding with new macOS SDK updates, currently at version 12.1.
Warnings
- breaking PyObjC frequently drops support for older Python versions. Version 12.0 dropped Python 3.9, and version 11.0 dropped Python 3.8. Always verify Python compatibility for your specific PyObjC version.
- breaking Major changes to PyObjC's Automatic Reference Counting (ARC) behavior, particularly for initializer (`init`) methods. PyObjC now correctly models that methods in the 'init' family steal a reference to self and return a new reference, aligning with Clang's ARC documentation.
- gotcha Interactions between Python's `__init__` and `__new__` methods and Objective-C object creation can be tricky. PyObjC 10.3 initially removed `__init__` support when PyObjC's `__new__` was used, which was partially re-introduced in 10.3.1.
- gotcha Experimental support for Python 3.13's free-threading (PEP 703) was introduced in PyObjC 11.0, but PyObjC 10.3 explicitly did not support it. Using PyObjC in a free-threaded environment may introduce instability or unexpected behavior.
- deprecated The `NSUserNotificationCenter` API, wrapped by this framework, is deprecated on macOS 10.14 and later in favor of `UserNotifications.framework` (`UNUserNotificationCenter`). While still functional, Apple recommends migrating to the newer API for future compatibility and features.
Install
-
pip install pyobjc-framework-notificationcenter -
pip install pyobjc
Imports
- NSUserNotificationCenter, NSUserNotification
from Foundation import NSDate from NotificationCenter import NSUserNotification, NSUserNotificationCenter
Quickstart
import objc
from Foundation import NSDate
from NotificationCenter import NSUserNotification, NSUserNotificationCenter
import time
# Create a notification
notification = NSUserNotification.alloc().init()
notification.setTitle_("PyObjC Notification")
notification.setInformativeText_("This is a test notification from NotificationCenter.framework.")
notification.setDeliveryDate_(NSDate.dateWithTimeIntervalSinceNow_(0)) # Deliver immediately
# Set an identifier to avoid duplicate notifications in Notification Center (optional, but good practice)
notification.setIdentifier_(f"com.example.pyobjc.notification.{time.time()}")
# Get the default notification center and deliver the notification
notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
notificationCenter.deliverNotification_(notification)
print("Notification delivered. Check your macOS Notification Center.")
# In a real application, you might need to run an event loop
# For simple scripts, exiting is usually sufficient as the notification
# delivery is handled by the system.