我编写了一个脚本,它生成这样的通知:
notify-send -i audio-card "Transferring audio playback to speakers." \
"Audio playback has been transferred to the analog output speaker system."有没有任何方法可以清除或替换命令行或Python中的这些通知?本质上,我有一个在两个PulseAudio接收器之间切换的脚本,如果用户快速切换,我希望清除以前这种类型的通知,用更新的文本替换它们。
发布于 2013-05-11 02:38:51
在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这将输出如下内容:
(uint32 72,)72是通知ID。现在您已经知道了ID,您可以用以下方法关闭弹出窗口:
gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.CloseNotification 72 现在,如果稍后需要ID,只需在调用Notify时将其写入文件:
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当你想要关闭弹出时,从那里得到它:
gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.CloseNotification $(cat /tmp/last_id)我在Gnome 3上,通过notify-send、pynotify、libnotify等发送的通知只持续5秒,而不考虑time选项(这是Gnome 3中的默认行为,不要问我为什么)。而且,它们不会叠加: Gnome一次只显示一个通知。所以我不能用几个弹出式窗口进行测试,但应该可以。
发布于 2013-05-10 20:24:34
使用update方法并再次调用show方法:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pynotify
import time
pynotify.init("Basic")
n = pynotify.Notification("Title1", "body1", "dialog-warning")
n.show()
time.sleep(1)
n.update("Title2", "body2", "dialog-warning")
n.show()有close方法但是..。它坏了。显然,不仅仅是为了我:
我认为它可能是由绑定中的错误引起的,所以我创建了这个C++程序:
#include <libnotify/notify.h>
#include <unistd.h>
int main(int argc, char **argv) {
GError *error = NULL;
notify_init("basic");
NotifyNotification *example;
example = notify_notification_new("Ttile1", "Body1", NULL);
notify_notification_show(example, &error);
usleep(1000000);
notify_notification_update(example, "Ttile2", "Body2", NULL);
notify_notification_show(example, &error);
usleep(1000000);
notify_notification_close(example, &error);
}并将其汇编如下:
g++ test.cpp -o test $(pkg-config --libs --cflags libnotify)也没那么好用。好像有什么东西被窃听了。
接下来,我尝试使用pickle模块来保存引用--它也不起作用。您不能存储对这些的引用。
所以我唯一能想到的就是把脚本分成两部分:守护进程和客户端。在运行之后,客户端应该与一直在后台运行的守护进程进行通信,并引用通知实例。
https://unix.stackexchange.com/questions/75386
复制相似问题