Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active September 3, 2023 16:51
Show Gist options
  • Save pudquick/8513781 to your computer and use it in GitHub Desktop.
Save pudquick/8513781 to your computer and use it in GitHub Desktop.
Notification in pyobjc
# Banner-style (default)
from Foundation import NSUserNotification, NSUserNotificationCenter
def notify(title, subtitle, text):
notification = NSUserNotification.alloc().init()
notification.setTitle_(str(title))
notification.setSubtitle_(str(subtitle))
notification.setInformativeText_(str(text))
notification.setSoundName_("NSUserNotificationDefaultSoundName")
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
# Usage for this style:
notify(...)
# For Alert-style (but has to be changed in Notification Center after first notification)
from Foundation import NSUserNotification, NSUserNotificationCenter, NSObject
class Notification(NSObject):
def notify(self, title, subtitle, text, url):
notification = NSUserNotification.alloc().init()
notification.setTitle_(str(title))
notification.setSubtitle_(str(subtitle))
notification.setInformativeText_(str(text))
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setUserInfo_({"action":"open_url", "value":url})
NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
def userNotificationCenter_didActivateNotification_(self, center, notification):
userInfo = notification.userInfo()
# Do something with it
# Usage for this style:
notify_obj = Notification.alloc().init()
notify_obj.notify(...)
# Credit to: http://stackoverflow.com/questions/12202983/working-with-mountain-lions-notification-center-using-pyobjc
@yusuf8ahmed
Copy link

Do you know how to make a notification using the new UserNotifications and UNUserNotificationCenter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment