如何在 dbus-monitor 中检测通知的发送者和目的地?

问题描述 投票:0回答:1

我的目标是过滤来自不同应用程序(主要来自不同浏览器窗口)的通知。

我发现在 dbus-monitor 的帮助下,我可以编写一个小脚本来过滤我感兴趣的通知消息。 过滤器脚本运行良好,但我有一个小问题:

我从

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

命令。我必须添加“destination=':1.40'”因为在 Ubuntu 20.04 上我总是收到两次相同的通知。

的以下输出
dbus-monitor --profile "interface='org.freedesktop.Notifications'"

说明理由:

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

如您所见,发件人 :1.277 首先发送到 :1.56,这将是发送到 :1.40 目的地的发件人。 (简直

notify-send hello
测试消息已发送)

我的脚本是这样工作的,但是每次系统启动时,我都必须检查目标号码并相应地修改我的脚本才能工作。

我有两个问题:

  1. 如何自动发现目标字符串? (上例中的:1.40)
  2. 如何防止系统发送同一条消息两次? (如果这个问题得到回答,那么第 1 点下的问题就变得毫无意义了。)
bash dbus ubuntu-20.04 sender-id
1个回答
0
投票

我不知道它是否直接解决了您的问题,但您可以使用此命令询问有关发件人/目的地的详细信息:

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

假设发件人/目的地仍然可用,它将打印如下内容:

 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
      )
   ]

或者如果您只想要进程 ID:

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

..这只给出:

method return time=1677407392.028717 sender=org.freedesktop.DBus -> destination=:1.156472 serial=3 reply_serial=2
   uint32 4854
© www.soinside.com 2019 - 2024. All rights reserved.