如何在notify-osd中强制新的通知显示而不等待先前的退出?

我已经创建了一个脚本(以及一个导致此脚本的.desktop快捷方式)来启动和停止xampp …

它会检查xampp的状态,并相应地启动或停止xampp。

现在我已经在脚本启动时分配了一个通知,显示“正在开始xampp …”或“正在停止xampp …”,然后当xampp启动或停止时,它会显示“Xampp started …”或“ Xampp停了……“

我已经使用notify-send来显示通知,如下面的脚本所示

现在问题是,在这里,第二个通知等待第一个消息然后弹出,即使xampp已经启动/停止。

我希望通过强制先前的通知在其生命周期完成之前退出来立即显示新通知。

当您立即激活/停用无线/网络时,可以看到这一点…

例如,选择启用无线时会出现“启用无线”,如果您立即选择禁用无线,则会出现“无线禁用”通知,而不等待“无线启用”通知完成其生命周期。

那我该怎么做呢?

#!/bin/sh SERVICE='proftpd' if ps ax | grep -v grep | grep $SERVICE > /dev/null then notify-send -i /opt/lampp/htdocs/xampp/img/logo-small.gif "Stopping XAMPP..." && gksudo /opt/lampp/lampp stop && notify-send -i /opt/lampp/htdocs/xampp/img/logo- small.gif "XAMPP Stoped." else notify-send -i /opt/lampp/htdocs/xampp/img/logo-small.gif "Starting XAMPP..." && gksudo /opt/lampp/lampp start && notify-send -i /opt/lampp/htdocs/xampp/img/logo-small.gif "XAMPP Started." fi 

notify-send手册页上,我发现–urgency = LEVEL-u其中级别低,正常,严重。

这有用吗? 使其至关重要?

此外,我尝试使用command- notify-send -u=critical"Testing"但该dint工作……它给出了错误 – 指定的未知紧急度criticalTesting。 已知的紧急程度:低,正常,关键。

或者如果我给命令notify-send -u=LOW"Testing"它给我错误缺少-u的参数

任何关系?

来自评论的信息,

出于某种原因,这是以一种奇怪的方式工作! 它会显示一个对话框,而不是“Starting xampp ..”和“Stopping xampp ..”部分的通知,然后显示“xampp started”或“xampp stopped”的通知……:/对话框应对用ok和取消按钮!

在此处输入图像描述

这个bug有一个补丁 – https://bugs.launchpad.net/ubuntu/+source/libnotify/+bug/257135?comments=all

@izx为补丁制作了一个ppa版本,所以安装现在很简单(谢谢izx!) – 如何使用’notify-send’立即替换现有通知?

要安装,请打开终端并:

 sudo apt-add-repository ppa:izx / askubuntu
 sudo apt-get update
 sudo apt-get install libnotify-bin 

现在您应该已经安装了修补版本的notify-send,现在可以替换并打印ID号,因此您只能为shell脚本使用一个通知框。 该程序现在具有-p-r选项,或者长语法为--print-id--replace-id


我写了一个基于原始脚本的脚本,它将使用此脚本,启动和停止通知显示,直到停止和开始显示,并重新使用相同的通知框,如果您已安装修补版本,请创建一个名为config.txt的文件config.txt并将数字0放入其中,然后将该文件放在与lampp.sh文件相同的文件夹中。

 #!/bin/sh SERVICE='proftpd' if ps ax | grep -v grep | grep $SERVICE > /dev/null then notify-send -t 0 -p -r `cat config.txt` -i /opt/lampp/htdocs/xampp/img/logo-small.gif "Stopping XAMPP ..." >config.txt && gksudo /opt/lampp/lampp stop && notify-send -r `cat config.txt` -t 5000 -i /opt/lampp/htdocs/xampp/img/logo-small.gif "XAMPP Stoped." else notify-send -t 0 -p -r `cat config.txt` -i /opt/lampp/htdocs/xampp/img/logo-small.gif "Starting XAMPP ..." >config.txt && gksudo /opt/lampp/lampp start && notify-send -r `cat config.txt` -t 5000 -i /opt/lampp/htdocs/xampp/img/logo-small.gif "XAMPP Started." fi 

编辑这是它在我的节目中的表现…… 在此处输入图像描述

您可以使用notify_notification_update,然后使用notify_notification_show来更新现有通知。

这是Python和PyGObject的一个例子:

 from time import sleep from gi.repository import Notify Notify.init(app_name = 'notification-update-example') notification = Notify.Notification.new("Notification", "Original message", None) notification.show() sleep(3) notification.update("More notification", "Updated message", None) notification.show() 

X-REF:
如何使用’notify-send’立即替换现有通知?

没有补丁,你可以简单地做

 #!/bin/bash for i in {0..100..10} do killall notify-osd notify-send "testing" $i sleep 0.5 done 

书签:
如何在notify-osd中强制新的通知显示而不等待先前的退出?
如何使用’notify-send’立即替换现有通知?