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])
@drmercer
Copy link

This doesn't work. 🙁 It gives me this error:

Exception: Error org.freedesktop.DBus.Error.InvalidArgs: Type of message, '(susssasa{ss}i)', does not match expected type '(susssasa{sv}i)'

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-send doesn't allow specifying a dict with variant values, so there's no way to use dbus-send to create freedesktop notifications.

@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