dbus notification server in python

Submitted by Fekete Zoltán on Thu, 02/10/2022 - 08:25

Ensure this to have it working:
- no other notification daemon runs in the session;


from pydbus import SessionBus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop

from pydbus.generic import signal

class Example(object):
  """
    <node>
      <interface name="org.freedesktop.Notifications">
        <method name='GetServerInformation'>
           <arg type='s' name='response' direction='out'/>
           <arg type='s' name='vendor' direction='out'/>
           <arg type='s' name='version' direction='out'/>
           <arg type='s' name='spec_version' direction='out'/>
        </method>
        <method name='GetCapabilities'>
           <arg type='s' name='response' direction='out'/>
        </method>
        <method name='Notify'>
           <arg type='s' name='app_name' direction='in'/>
           <arg type='u' name='replaces_id' direction='in'/>
           <arg type='s' name='app_icon' direction='in'/>
           <arg type='s' name='summary' direction='in'/>
           <arg type='s' name='body' direction='in'/>
           <arg type='as' name='actions' direction='in'/>
           <arg type='a{sv}' name='hints' direction='in'/>
           <arg type='i' name='expire_timeout' direction='in'/>
           <arg type='u' name='response' direction='out'/>
        </method>
      </interface>
    </node>
  """

  messageId = 0

  def GetServerInformation(self):
      return ("zoli-server", "Minux", "0.1", "1.2")

  def GetCapabilities(self):
      return ["body"]

  def Notify (self,
              app_name,    
              replaces_id,    
              app_icon,    
              summary,    
              body,    
              actions,    
              hints,    
              expire_timeout):

      print(Example.messageId, ":", summary, ":", body)

      Example.messageId += 1

      return Example.messageId
     

  def __init__(self):
      pass

DBusGMainLoop(set_as_default=True)
bus = SessionBus()
BUS = "org.freedesktop.Notifications"

bus.publish(BUS, Example())

loop = GLib.MainLoop()
loop.run()