在 Ubuntu 22.04.2 中显示通知时触发事件

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

我的代码来自有用的文章、谷歌、聊天 gpt 等的组合。该脚本应该在显示通知时执行某些操作。一旦脚本实际运行,我希望它能够播放声音。我可以做那部分。然而该脚本什么也没做。我可以发送通知发送“hello”。正如您所期望的,通知显示在 ubuntu 中的时间下方,但运行脚本的终端窗口不执行任何操作。 我似乎无法让脚本挂钩通知事件。 我已经尝试以 sudo 身份运行该脚本,但它仍然不起作用。 我需要在某处授予权限吗?

非常感谢

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
import dbus
from dbus.mainloop.glib import DBusGMainLoop

def notifications(bus, message):
    print("something happened")
    if message.get_member() == "Notify":
        args = message.get_args_list()
        print(args)

DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()
proxy = bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
iface = dbus.Interface(proxy, "org.freedesktop.Notifications")
iface.connect_to_signal("NotificationClosed", notifications)
iface.connect_to_signal("ActionInvoked", notifications)

mainloop = GLib.MainLoop()
mainloop.run()
python ubuntu notifications dbus
1个回答
0
投票

如果您仔细查看自己的代码,您会注意到它没有将任何处理程序附加到“显示的通知”信号,仅附加到“通知关闭”信号。

更重要的是,根本没有“通知显示”信号——通知守护进程实际上不发送信号。只有一个“Notify”方法调用发送到通知服务(而不是from),并且方法调用不会像信号那样广播;他们总是被定向到特定的服务。您无法接收定向到不同总线名称的消息。 (当然,即使您确实收到了方法调用,connect_to_signal() 也会忽略它,因为它不是信号。) dbus-monitor 的做法是将其总线连接切换到特殊的“监视器”模式(类似于 tcpdump 中的以太网混杂模式)。如果运行 dbus-monitor 两次,您会看到它对

BecomeMonitor

对象发出

/org/freedesktop/DBus
调用:
‣ Type=method_call  Endian=l  Flags=0  Version=1 Cookie=2  Timestamp="Sat 2023-08-05 12:27:52.091283 UTC"
  Sender=:1.313  Destination=org.freedesktop.DBus  Path=/org/freedesktop/DBus
    Interface=org.freedesktop.DBus.Monitoring  Member=BecomeMonitor
  UniqueName=:1.313
  MESSAGE "asu" {
          ARRAY "s" {
          };
          UINT32 0;
  };

‣ Type=method_return  Endian=l  Flags=1  Version=1 Cookie=3  ReplyCookie=2  Timestamp="Sat 2023-08-05 12:27:52.091345 UTC"
  Sender=org.freedesktop.DBus  Destination=:1.313
  MESSAGE "" {
  };

© www.soinside.com 2019 - 2024. All rights reserved.