我知道这是一个老问题 - 但可能还有人在寻找一个答案如何在Python & PyObjC为此,我将在这里发表正确的语法(因为它是谷歌最好的结果之一)。由于我不能评论小鬼的评论,我会张贴这个作为另一个答案。
很可能这样做同样在Python与pyobjc:
def userNotificationCenter_shouldPresentNotification_(self, center, notification):
return True
满级是这样的:
from Cocoa import NSObject
import objc
class MountainLionNotification(NSObject):
""" MacOS Notifications """
# Based on http://stackoverflow.com/questions/12202983/working-with-mountain-lions-notification-center-using-pyobjc
def userNotificationCenter_shouldPresentNotification_(self, center, notification):
return True
def init(self):
self = super(MountainLionNotification, self).init()
if self is None: return None
# Get objc references to the classes we need.
self.NSUserNotification = objc.lookUpClass('NSUserNotification')
self.NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
return self
def clearNotifications(self):
"""Clear any displayed alerts we have posted. Requires Mavericks."""
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()
def notify(self, title="", subtitle="", text="", url=""):
"""Create a user notification and display it."""
notification = self.NSUserNotification.alloc().init()
notification.setTitle_(str(title))
notification.setSubtitle_(str(subtitle))
notification.setInformativeText_(str(text))
# notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setHasActionButton_(False)
notification.setActionButtonTitle_("View")
# notification.setUserInfo_({"action":"open_url", "value": url})
self.NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
self.NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
# Note that the notification center saves a *copy* of our object.
return notification
您可以加入一个完整的样本,我不知道到明白该放哪一行。 – 2013-05-19 09:10:35
非常感谢。有趣的是,如果您不设置委托并实施此方法,通知在通知中心中可见,但不会显示。 – Ants 2013-06-26 02:48:43
@Ants如何在python中设置委托 – imp 2013-11-18 04:42:58