Last active
September 3, 2023 16:51
-
-
Save pudquick/8513781 to your computer and use it in GitHub Desktop.
Notification in pyobjc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know how to make a notification using the new UserNotifications and UNUserNotificationCenter