I'm trying to test a simple Python script to send out a macOS notification:
import UserNotifications
def notif_callback(err):
print("Error in notification callback:",err)
def auth_callback(granted, err):
print("Granted: ",granted,)
print("Error in authorization request: ",err)
content=UserNotifications.UNMutableNotificationContent.alloc().init()
content.setTitle_("Test")
r=UserNotifications.UNNotificationRequest.requestWithIdentifier_content_trigger_('test_notification',content,None)
c=UserNotifications.UNUserNotificationCenter.currentNotificationCenter()
c.requestAuthorizationWithOptions_completionHandler_(0b111111,auth_callback)
c.addNotificationRequest_withCompletionHandler_(r,notif_callback)
However, when I tried to run the program, it gives the following errors
Granted: False
Error in authorization request: Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
Error in notification callback: Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
I have not seen any notification authorization from my system and it seems the OS automatically denied the request. In the system preferences, Python has been granted all notification permissions. What am I missing here?
Only code-signed applications will be granted authorisation to send user notifications through UNUserNotificationCenter. I believe that this requirement is new and does not apply to NSUserNotificationCenter.
Edit: This is actually quite easily achieved. You just just need a signed Framework build of Python, as provided by the official installers from python.org. AFAIK installs via Homebrew or most other channels are not signed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With