PyObjC "Notifications are not allowed for this application"
Asked Answered
M

1

3

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?

Martens answered 6/8, 2019 at 17:43 Comment(1)
if anyone has a working answer please post: #62234533Candra
F
6

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.

Flavescent answered 1/10, 2019 at 21:34 Comment(3)
Thanks for the info. Where can I find content regarding this rule you mentioned?Implausibility
I'm not sure if this is documented anywhere. Apps build with Xcode are typically signed, even if only with an ad-hoc certificate, so it likely won't come up as an issue in an Apple developer forum. There is a note in the Xamarin docs, of all places: "With MacOS apps, for the permission dialog to appear, you must sign your macOS app, even if building locally in DEBUG mode."Flavescent
This is correct. The example OP posted "just works" on MacOS Python from python.orgBushed

© 2022 - 2024 — McMap. All rights reserved.