我正在尝试测试一个简单的Python脚本来发出macOS通知:
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
但是,当我尝试运行该程序时,它出现以下错误
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授予了所有通知权限。我在这里想念什么?
答案 0 :(得分:0)
将仅授权经过代码签名的应用程序通过UNUserNotificationCenter
发送用户通知。我认为这项要求是新的,不适用于NSUserNotificationCenter
。