remotenotify/app.py
2025-09-17 11:37:51 +02:00

53 lines
1.5 KiB
Python

#import dbus
#bus_local = dbus.bus.BusConnection('unix:path=/run/user/1000/bus')
#bus_remote = dbus.bus.BusConnection('unix:path=/run/user/1000/bus-remote')
# https://stackoverflow.com/a/75122112
# https://pychao.com/2021/03/01/sending-desktop-notification-in-linux-with-python-with-d-bus-directly/
import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
from pprint import pprint
def msg_cb(bus, msg):
if msg.get_interface() == 'org.freedesktop.Notifications' and msg.get_member() == 'Notify':
args = msg.get_args_list()
print("Notification from '%s'" % args[0])
print("Summary: %s" % args[3])
print("Body: %s" % args[4])
bus_remote = dbus.bus.BusConnection('unix:path=/run/user/1000/bus-remote')
item = "org.freedesktop.Notifications"
notfy_intf = dbus.Interface(
bus_remote.get_object(item, "/"+item.replace(".", "/")), item)
notfy_intf.Notify(
args[0], 0, "", args[3], args[4],
[], {"urgency": 1}, -1)
def main():
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
obj_dbus = bus.get_object('org.freedesktop.DBus',
'/org/freedesktop/DBus')
obj_dbus.BecomeMonitor(["interface='org.freedesktop.Notifications'"],
dbus.UInt32(0),
interface='org.freedesktop.Notifications')
bus.add_message_filter(msg_cb)
print('start')
mainloop = GLib.MainLoop()
mainloop.run()
if __name__ == '__main__':
main()