PyObjC PushKit Framework
pyobjc-framework-pushkit provides Python wrappers for the PushKit framework on macOS, allowing Python applications to interact with Apple's PushKit services, such as VoIP push notifications. The library is currently at version 12.1 and typically releases new versions in alignment with macOS SDK updates, often with several releases per year to incorporate new features, bug fixes, and Python version support.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Ensure your project uses Python 3.10 or later.
- breaking PyObjC 11.0 dropped support for Python 3.8. Additionally, version 11.1 introduced changes to how ARC behavior is modeled for initializer methods, aligning with `clang`'s documentation. This can affect custom Objective-C class subclasses in Python.
- gotcha PyObjC applications deeply integrate with the macOS Cocoa event loop. For console applications or scripts, you must explicitly run an event loop (e.g., using `PyObjCTools.AppHelper.runConsoleEventLoop()`) for delegate methods and other asynchronous Cocoa events to be processed.
- gotcha PyObjC wraps Objective-C APIs directly, meaning Python code must adhere to Objective-C conventions (e.g., delegate patterns, snake_case_for_methods_ with_underscores_representing_colons, specific object lifecycle management) which can be unfamiliar to Python-only developers.
- gotcha PushKit requires specific registration steps: create a `PKPushRegistry` object at launch, keep a reference to it, and assign its delegate *before* setting `desiredPushTypes` to initiate registration. Incorrect ordering or failure to retain the registry object can lead to pushes not being received.
Install
-
pip install pyobjc-framework-pushkit
Imports
- PKPushRegistry
from PushKit import PKPushRegistry
- PKPushRegistryDelegate
from PushKit import PKPushRegistryDelegate
- PKPushTypeVoIP
from PushKit import PKPushTypeVoIP
Quickstart
import objc
from Foundation import NSObject, NSLog, NSSet
from PushKit import PKPushRegistry, PKPushRegistryDelegate, PKPushTypeVoIP
from PyObjCTools import AppHelper
class PushDelegate(NSObject):
# Adopting the PKPushRegistryDelegate protocol
def init(self):
self = objc.super(PushDelegate, self).init()
if self is None:
return None
NSLog('PushDelegate initialized.')
return self
def pushRegistry_didUpdatePushCredentials_forType_(self, registry, credentials, pushType):
NSLog(f'pushRegistry:didUpdatePushCredentials:forType_ called for type: {pushType}, token: {credentials.token()}')
# In a real app, send credentials.token() to your server
# for APNs registration.
def pushRegistry_didReceiveIncomingPushWithPayload_forType_completionHandler_(self, registry, payload, pushType, completion):
NSLog(f'pushRegistry:didReceiveIncomingPushWithPayload:forType:completionHandler_ called for type: {pushType}, payload: {payload}')
# Process the incoming push notification payload.
# Call the completion handler when done.
completion()
def pushRegistry_didInvalidatePushTokenForType_error_(self, registry, pushType, error):
NSLog(f'pushRegistry:didInvalidatePushTokenForType:error_ called for type: {pushType}, error: {error}')
def main():
# Initialize an NSApplication (required for delegate pattern)
# For a console app, you might not explicitly use NSApplication.sharedApplication(),
# but the runloop expects a Cocoa environment.
# from AppKit import NSApplication # Uncomment if you need direct AppKit interaction
# Create the push registry delegate
delegate = PushDelegate.alloc().init()
# Create a PKPushRegistry object
# initWithQueue:nil means callbacks happen on the main thread
# PKPushRegistry requires a queue, typically the main queue for UI apps
# For a simple console app, we can use nil to imply the main thread's runloop.
push_registry = PKPushRegistry.alloc().initWithQueue_(None)
push_registry.setDelegate_(delegate)
# Register for VoIP push notifications
push_registry.setDesiredPushTypes_(NSSet.setWithObject_(PKPushTypeVoIP))
NSLog('PushKit registration initiated. Entering app event loop.')
# Run the application's event loop. This is crucial for receiving delegate callbacks.
# In a real app, this would be part of the main NSApplication runloop.
# For a simple console demonstration, AppHelper.runConsoleEventLoop() is used.
try:
AppHelper.runConsoleEventLoop(installInterrupt=True)
except KeyboardInterrupt:
NSLog('Application terminated by user.')
AppHelper.stopEventLoop()
if __name__ == '__main__':
main()