PyObjC “Notifications are not allowed for this application”

隐身守侯 提交于 2020-06-17 09:54:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!