Last active
March 10, 2020 17:26
-
-
Save baverman/6440898 to your computer and use it in GitHub Desktop.
Simple libnotify implementation using only plain dbus-send. Can close created notification.
This file contains hidden or 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
| import sys | |
| from subprocess import Popen, PIPE | |
| def notify(title, body=None, timeout=-1, appname='simple-notify'): | |
| cmd = [ | |
| 'dbus-send', | |
| '--type=method_call', | |
| '--print-reply=literal', | |
| '--dest=org.freedesktop.Notifications', | |
| '/org/freedesktop/Notifications', | |
| 'org.freedesktop.Notifications.Notify', | |
| 'string:{}'.format(appname), | |
| 'uint32:0', | |
| 'string:""', | |
| 'string:{}'.format(title), | |
| 'string:{}'.format(body), | |
| 'array:string:""', | |
| 'dict:string:string:"",""', | |
| 'int32:{}'.format(timeout), | |
| ] | |
| out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() | |
| if err: | |
| raise Exception(err) | |
| return int(out.strip().split()[1]) | |
| def close(nid): | |
| cmd = [ | |
| 'dbus-send', | |
| '--type=method_call', | |
| '--dest=org.freedesktop.Notifications', | |
| '/org/freedesktop/Notifications', | |
| 'org.freedesktop.Notifications.CloseNotification', | |
| 'int32:{}'.format(nid), | |
| ] | |
| out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() | |
| if err: | |
| raise Exception(err) | |
| if __name__ == '__main__': | |
| if sys.argv[1] == 'close': | |
| close(sys.argv[2]) | |
| else: | |
| print notify(*sys.argv[1:4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work. 🙁 It gives me this error:
As used here, the values in the dict are strings, but that doesn't match the desktop notifications spec, which says that the hints dict should have variant values.
Sadly, it seems
dbus-senddoesn't allow specifying a dict with variant values, so there's no way to usedbus-sendto create freedesktop notifications.