How to detect sender and destination of a notification in dbus-monitor?

1.8k views Asked by At

My goal is to filter notifications coming from different applications (mainly from different browser window).

I found that with the help of the dbus-monitor I can write a small script that could filter the notification messages that I am interested in. The filter script is working well, but I have a small problem:

I am starting with the

dbus-monitor "interface='org.freedesktop.Notifications', destination=':1.40'"

command. I have to added the "destination=':1.40'" because on Ubuntu 20.04 I always got twice the same notification. The following output of

dbus-monitor --profile "interface='org.freedesktop.Notifications'"

demonstrate the reason:

type   timestamp               serial  sender  destination     path                            interface               member
#                                       in_reply_to
mc      1612194356.476927       7       :1.227  :1.56   /org/freedesktop/Notifications  org.freedesktop.Notifications   Notify
mc      1612194356.483161       188     :1.56   :1.40   /org/freedesktop/Notifications  org.freedesktop.Notifications   Notify

As you can see the sender :1.277 sends to :1.56 first than this will be the sender to :1.40 destination. (Simply notify-send hello test message was sent)

My script is working on that way, but every time system boot up, I have to check the destination number and modify my script accordingly to get worked.

I have two questions:

  1. how to discover the destination string automatically? (:1.40 in the above example)
  2. how to prevent system sending the same message twice? (If this question would be answered, than the question under point 1. became pointless.)
1

There are 1 answers

0
Jonas Berlin On

I don't know if it directly solves your problem, but you can ask for details about a sender/destination using this command:

$ dbus-send --print-reply --dest=org.freedesktop.DBus \
  /org/freedesktop/DBus \
  org.freedesktop.DBus.GetConnectionCredentials \
  string:':1.277'

Assuming the sender/destination is still available, it will print something like:

 method return time=1677403493.492844 sender=org.freedesktop.DBus -> destination=:1.156069 serial=3 reply_serial=2
   array [
      dict entry(
         string "ProcessID"
         variant             uint32 4854
      )
      dict entry(
         string "UnixUserID"
         variant             uint32 502
      )
   ]

Or if you want just the process id:

$ dbus-send --session --print-reply --dest=org.freedesktop.DBus \
  /org/freedesktop/DBus \
  org.freedesktop.DBus.GetConnectionUnixProcessID \
  string:':1.277'

..which gives just:

method return time=1677407392.028717 sender=org.freedesktop.DBus -> destination=:1.156472 serial=3 reply_serial=2
   uint32 4854

These and others are documented here: https://dbus.freedesktop.org/doc/dbus-specification.html