PyObjC“此应用程序不允许通知"; [英] PyObjC "Notifications are not allowed for this application"
问题描述
我正在尝试测试一个简单的 Python 脚本来发送 macOS 通知:
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}
我没有看到来自我的系统的任何通知授权,似乎操作系统自动拒绝了该请求.在系统首选项中,Python 已被授予所有通知权限.我在这里错过了什么?
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?
推荐答案
只有代码签名的应用程序才会被授权通过 UNUserNotificationCenter
发送用户通知.我相信这个要求是新的,不适用于 NSUserNotificationCenter
.
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
.
这实际上很容易实现.您只需要一个签名的 Python 框架构建,由 python.org 的官方安装程序提供.通过 Homebrew 或大多数其他渠道安装的 AFAIK 未签名.
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.
这篇关于PyObjC“此应用程序不允许通知";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!