Cannot import name UN UserNotificationCenter Delegate

I’m using VSCode on mac os (12.7.6). python is 3.10.11. when trying use objc to import
UserNotifications . I got an error;

ImportError: cannot import name ‘UNUserNotificationCenterDelegate’ from ‘UserNotifications’ (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/UserNotifications/init.py)

Open that file,

"""
Python mapping for the UserNotifications framework.

This module does not contain docstrings for the wrapped code, check Apple's
documentation for details on how to use these functions and classes.
"""


def _setup():
    import sys

    import Foundation
    import objc
    from . import _metadata, _UserNotifications

    dir_func, getattr_func = objc.createFrameworkDirAndGetattr(
        name="UserNotifications",
        frameworkIdentifier="com.apple.UserNotifications",
        frameworkPath=objc.pathForFramework(
            "/System/Library/Frameworks/UserNotifications.framework"
        ),
        globals_dict=globals(),
        inline_list=None,
        parents=(
            _UserNotifications,
            Foundation,
        ),
        metadict=_metadata.__dict__,
    )

    globals()["__dir__"] = dir_func
    globals()["__getattr__"] = getattr_func

    for cls, sel in (
        ("UNNotificationActionIcon", b"init"),
        ("UNUserNotificationCenter", b"init"),
        ("UNNotificationCategory", b"init"),
        ("UNNotificationRequest", b"init"),
        ("UNNotificationTrigger", b"init"),
        ("UNNotificationAttachment", b"init"),
        ("UNNotificationAction", b"init"),
        ("UNNotificationSound", b"init"),
        ("UNNotificationResponse", b"init"),
        ("UNNotificationSettings", b"init"),
    ):
        objc.registerUnavailableMethod(cls, sel)

    del sys.modules["UserNotifications._metadata"]


globals().pop("_setup")()

how to add UNUserNotificationCenterDelegate

The exact position doesn’t matter, but I’d put it below the entry for UNUserNotificationCenter:

    for cls, sel in (
        ("UNNotificationActionIcon", b"init"),
        ("UNUserNotificationCenter", b"init"),
        ("UNUserNotificationCenterDelegate", b"init"), # <-- Added here

Just remember to check that it doesn’t break anything.

If it does, remove it, and look for another way to do whatever it is you’re trying to do, assuming that it’s possible.

after added “(“UNUserNotificationCenterDelegate”, b"init”)," to file __init__.py as your suggest,
the error still

    globals()["__dir__"] = dir_func
    globals()["__getattr__"] = getattr_func

    for cls, sel in (
        ("UNNotificationActionIcon", b"init"),
        ("UNUserNotificationCenter", b"init"),
        ("UNUserNotificationCenterDelegate", b"init"),
        ("UNNotificationCategory", b"init"),
        ("UNNotificationRequest", b"init"),
        ("UNNotificationTrigger", b"init"),
        ("UNNotificationAttachment", b"init"),
        ("UNNotificationAction", b"init"),
        ("UNNotificationSound", b"init"),
        ("UNNotificationResponse", b"init"),
        ("UNNotificationSettings", b"init"),
    ):
        objc.registerUnavailableMethod(cls, sel)

    del sys.modules["UserNotifications._metadata"]


globals().pop("_setup")()

I don’t know why. but , when i removed the reference to UNUserNotificationCenterDelegate, and remove class FileSaveDelegate , trigger , request, run the py file, there is no error. it means UNMutableNotificationContent can be recognized, but it is not in __init__.py file.

if i add trigger and request , there is another error

TypeError: UNTimeIntervalNotificationTrigger() does not support keyword arguments ‘triggerWithTimeInterval’, ‘repeats’

i can only upload on image,

What is the code you are trying to run and what is the error message?

UNUserNotificationCenterDelegate is an Objective-C protocol, and those are not exposed as importable names at the moment (for historical reasons). You can use objc.protocolNamed("UNUserNotificationCenterDelegate") instead.

Thanks. it’s really can solve the problem of import failure