PyObjC PushKit Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `PKPushRegistry` and register for VoIP push notifications using `pyobjc-framework-pushkit`. It defines a delegate class to handle push registration updates and incoming notifications, then starts a console event loop to process these events. This code requires a macOS environment and an application bundle that has PushKit capabilities enabled.

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()

view raw JSON →