Skip to content

Instantly share code, notes, and snippets.

@baverman

baverman/simple_notify.py

Last active Mar 10, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@drmercer drmercer commented Oct 22, 2019

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

This comment has been minimized.

Copy link

@mikemaksymowych mikemaksymowych commented Mar 10, 2020

You can use gdbus call, though

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