pkq*_*xdd 3 pyobjc objective-c python-3.7 macos-mojave
我正在尝试测试一个简单的 Python 脚本来发送 macOS 通知:
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)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行该程序时,出现以下错误
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}
Run Code Online (Sandbox Code Playgroud)
我没有看到我的系统有任何通知授权,操作系统似乎自动拒绝了该请求。在系统偏好设置中,Python已被授予所有通知权限。我在这里缺少什么?
只有代码签名的应用程序才会被授予通过 发送用户通知的授权UNUserNotificationCenter。我认为这个要求是新的,不适用于NSUserNotificationCenter.
编辑:这实际上很容易实现。您只需要一个签名的 Python 框架版本,由python.org的官方安装程序提供。AFAIK 通过 Homebrew 或大多数其他渠道的安装都没有签名。