问题
I'm trying to test a simple Python script to send out a macOS notification:
import objc
import UserNotifications
from PyObjCTools import AppHelper
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)
input() # suspend the program
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?
回答1:
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
.
来源:https://stackoverflow.com/questions/57381422/pyobjc-notifications-are-not-allowed-for-this-application