如何使用“通知发送”立即替换现有通知?

ish*_*ish 29 notification command-line bash notify-osd

当我使用notify-send在桌面上显示通知,然后再次使用它来显示不同的通知时,我注意到第二个仅第一个消失后才显示。

有没有办法notify-send立即用不同的通知替换现有通知?

ish*_*ish 22

你可以,但你必须使用一个补丁libnotify这样做

notify-send无法在超时(或消失)之前替换现有通知。这是一个已知的错误。但是,错误报告的评论者发布了一个补丁来修复它。

从 PPA安装打补丁的libnotify-bin

我已经创建了libnotify-bin包的补丁版本,它允许在我的 PPA 中进行替换。目前它仅适用于 Ubuntu 12.04,但如果您需要它用于当前支持的任何其他版本,请发表评论,我会尽力使其可用。

要安装,请打开终端并:

须藤 apt-add-repository ppa:izx/askubuntu
sudo apt-get 更新
须藤 apt-get 安装 libnotify-bin

如何使用替换功能

修补程序notify-send包括两个新开关,-p(或--print-id)和-r(或--replace-id)。将--help它们描述为:

  -p, --print-id 打印通知 ID。
  -r, --replace-id=REPLACE_ID 要替换的通知的 ID。
  • 使用-p,每个都notify-send将返回一个 ID N(数字/整数)。
  • 发出另一个notify-sendwith-r N立即替换之前的通知。
  • 例如,对于 bash,您可以使用以下命令保存 ID notify-send -p ...

    NID=$(notify-send -p "MESSAGE-1")
    
    Run Code Online (Sandbox Code Playgroud)

    然后将其替换为:

    notify-send -r $NID "MESSAGE-2"
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以在脚本中递归使用-p-r,只要-r变量在开始时初始化为 0。

  • 这是一个简单的脚本,显示以半秒为间隔从 0 到 100 计数的通知:

    #!/bin/bash 
    NID=0
    对于我在 {0..100..10}    做       NID=$(notify-send -p -r $NID $i)       睡眠 0.5    完毕


Mar*_*k L 14

您可以使用“同步”提示创建一个“确认”通知,该通知将替换之前的确认通知。例如:

notify-send "Message" -h string:x-canonical-private-synchronous:anything
Run Code Online (Sandbox Code Playgroud)

本文档中指定了“x-canonical-private-synchronous”提示。要指定提示,请使用-h type:name:value. 这里的类型是string,名称是x-canonical-private-synchronous,看起来值可以是任何你想要的。

因此,如果您的第一个通知是使用该提示创建的,第二个也是如此,则第二个将立即替换第一个。(请参阅文档中的动画和持续时间,在“确认气泡”列中。)

  • 它不适用于 GNOME.... (3认同)

geo*_*ell 10

X-ref:
如何在notify-osd 中强制显示新通知而无需等待较早的通知退出?

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

#!/bin/bash

for i in {0..100..10}
    do
          killall notify-osd
          notify-send "testing" $i
          sleep 1
    done
Run Code Online (Sandbox Code Playgroud)

书签:
如何使用“通知发送”立即替换现有通知?


发送错误通知 osd(2592):不允许操作。这意味着什么?

这可能意味着特权不足,需要:

sudo killall notify-osd
Run Code Online (Sandbox Code Playgroud)


小智 5

我创建了一个简单的 python 脚本,它的工作方式与 notify-send 几乎相同,但支持--replaces-id.

通知发送.py

网站: https : //github.com/phuhl/notify-send.py

用于从 shell 发送桌面通知的 python 脚本。

关于

Libnotify 是 Linux 世界中许多脚本的一部分。它利用了桌面通知规范的许多指定功能,并使 shell 脚本可以访问它们。它但是允许替换与现有通知replaces-id。这是自 2008 年以来的已知错误,自 2012 年以来有一个补丁。尽管该补丁仍然不是上游(2018 年)。

此 python 脚本利用notify2包并将功能公开给 shell。

notify-send.py 和 notify-send 的区别

  • Innotify-send.py -h 显示帮助而不是提示的参数。对于提示使用--hint.
  • notify-send.py -r IDnotify-send.py --replaces-id ID 存在。为了用要替换的通知notify-send.py 返回的 ID替换通知调用。
  • notify-send.py 返回新创建的通知的 ID。
  • notify-send.py --replaces-process NAME存在。使用相同 NAME 创建的每个通知都将使用相同的 NAME 替换它之前的每个通知。如果使用此参数调用notify-send.py可能会阻塞,最好使用尾随&.

安装

需要python3。

git clone https://github.com/phuhl/notify-send.py
cd notify-send.py
sudo pip install notify2
sudo python setup.py install
Run Code Online (Sandbox Code Playgroud)

用法

$ notify-send.py -h
usage: notify-send.py [-h] [-u LEVEL] [-t TIME] [-a APP_NAME]
                  [-i ICON[,ICON...]] [-c TYPE[,TYPE...]]
                  [--hint TYPE:NAME:VALUE] [-r ID]
                  [--replaces-process NAME]
                  SUMMERY [BODY]

positional arguments:
      SUMMERY
      BODY

optional arguments:
      -h, --help        show this help message and exit
      -u LEVEL, --urgency LEVEL
                        Specifies the urgency level (low, normal, critical).
      -t TIME, --expire-time TIME
                        Specifies the timeout in milliseconds at which to
                        expire the notification.
      -a APP_NAME, --app-name APP_NAME
                        Specifies the app name for the icon
      -i ICON[,ICON...], --icon ICON[,ICON...]
                        Specifies an icon filename or stock icon to display.
      -c TYPE[,TYPE...], --category TYPE[,TYPE...]
                        Specifies the notification category.
      --hint TYPE:NAME:VALUE
                        Specifies basic extra data to pass. Valid typesare
                        int, double, string and byte.
      -r ID, --replaces-id ID
                        Specifies the id of the notification that should be
                        replaced.
     --replaces-process NAME
                        Specifies the name of a process that should take care
                        of replacing notifications for this process.
Run Code Online (Sandbox Code Playgroud)

以 root 用户身份通知发送 [.py]

为了显示通知,即使 libnotify 或

notify-send.py
Run Code Online (Sandbox Code Playgroud)

从 root 用户使用这两个脚本很有帮助。

#!/bin/bash
username=<your username here>
if [ "$(id -u)" != "1000" ] ; then
    sudo -u $username DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send.sh "$@"
else
    notify-send.sh "$@"
fi
Run Code Online (Sandbox Code Playgroud)

有了 notify-send.sh这样的:

#!/bin/bash
notify-send.py "$@" &
Run Code Online (Sandbox Code Playgroud)

也可以看看

也看看我的通知守护程序Dunst启发,但有一些改进,包括透明背景和存储通知的通知中心的可能性。