Skip to content

Instantly share code, notes, and snippets.

@baverman
Last active March 10, 2020 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baverman/6440898 to your computer and use it in GitHub Desktop.
Save baverman/6440898 to your computer and use it in GitHub Desktop.
Simple libnotify implementation using only plain dbus-send. Can close created notification.
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])
@mikemaksymowych
Copy link

You can use gdbus call, though

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