清除/修改以前的 libnotify 通知?

Naf*_*Kay 8 python notifications libnotify

我写了一个脚本来生成这样的通知:

notify-send -i audio-card "Transferring audio playback to speakers." \
    "Audio playback has been transferred to the analog output speaker system."
Run Code Online (Sandbox Code Playgroud)

有什么方法可以从命令行或 Python 清除或替换这些通知?本质上,我有一个脚本可以在两个 PulseAudio 接收器之间切换,如果用户快速切换,我想清除以前的此类通知以将它们替换为更新的文本。

don*_*sti 8

在 CLI 中,您可以通过gdbus/显示和关闭通知弹出窗口qdbus
这是如何做到的gdbus

gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify my_app_name 42 audio-card "Message" "Body" [] {} 20
Run Code Online (Sandbox Code Playgroud)

这将输出如下内容:

(uint32 72,)
Run Code Online (Sandbox Code Playgroud)

72作为通知ID。现在您知道了,ID您可以使用以下命令关闭弹出窗口:

(uint32 72,)
Run Code Online (Sandbox Code Playgroud)

现在,如果您需要ID稍后,只需在调用时将其写入文件Notify

gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.CloseNotification 72 
Run Code Online (Sandbox Code Playgroud)

并在您想关闭弹出窗口时从那里获取它:

gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify my_app_name 42 audio-card "Message" "Body" [] {} 20  | sed 's/[^ ]* //; s/,.//' > /tmp/last_id
Run Code Online (Sandbox Code Playgroud)

PS我在Gnome的3和通知通过发送notify-sendpynotifylibnotify等只剩下最后不管5秒time选项(这是在侏儒3的默认行为,不要问我为什么)。此外,它们不会叠加:Gnome 一次只显示一个通知。所以我不能用几个弹出窗口进行测试,但它应该可以工作。