PyObjC NotificationCenter Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to send a simple user notification using the `NSUserNotification` and `NSUserNotificationCenter` APIs provided by `NotificationCenter.framework` on macOS. Note that for modern macOS (10.14+), Apple recommends `UserNotifications.framework` (UNUserNotificationCenter), which is available via `pyobjc-framework-UserNotifications`.

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.

view raw JSON →