如何发送自定义桌面通知?


97

我有一个自定义脚本,我想发送一个带有自定义消息的桌面通知(出现在屏幕右上角的通知)。我怎么做?

Answers:


149

还有很多其他很酷的功能 notify-send

我们可以运行命令并将其显示在通知中:

notify-send <title> <`command`>
notify-send Date "`date`"
notify-send Disk "`df / -H`"

我们可以在通知中使用图标

notify-send -i <icon> <Message>
notify-send -i face-wink "Hello! January"

真烦人弹出

notify-send  -t 0 "Bringing down the system"

notify-send <title> <message>
notify-send "who am i" "I am January"

有关更多选项,请点击此处


1
谢谢。我们在哪里可以获得图标列表,例如face-wink您使用过的图标?
帕迪·兰道


notify-send -t 0有效,但notify-send "who am i" "I am January"不起作用:(-在Ubuntu 15.10上
AlikElzin-kilaka,2016年

3
--urgency=critical
AlikElzin-kilaka合作,2016年

在debian上安装sudo apt install libnotify-bin
user149244

18

只是为了增加其他答案,当从cron本地运行命令时,我使用

DISPLAY=:0.0 /usr/bin/notify-send "TITLE" "MESSAGE"


2

我创建了一个简单的,几乎是本机的脚本,该脚本播放声音并为Ubuntu(Gist)显示带有给定消息和时间的通知:

#!/bin/sh

# https://gist.github.com/John-Almardeny/04fb95eeb969aa46f031457c7815b07d
# Create a Notification With Sound with a Given Message and Time
# The Downloaded Sound is from Notification Sounds https://notificationsounds.com/

MSSG="$1"
TIME="$2"

# install wget if not found
if ! [ -x "$(command -v wget)" ]; then 
    echo -e "INSTALLING WGET...\n\n"
    sudo apt-get install wget
    echo -e "\n\n"
fi

# install at package if not found
if ! [ -x "$(command -v at)" ]; then
    echo -e "INSTALLING AT...\n\n"
    sudo apt-get install at
    echo -e "\n\n"
fi

# install sox if not found
if ! [ -x "$(command -v sox)" ]; then
    echo -e "INSTALLING SOX...\n\n"
    sudo apt-get install sox
    sudo apt-get install sox libsox-fmt-all
    echo -e "\n\n"
fi

# download the noti sound if this is first time
# add alias to the bashrc file
if ! [ -f ~/noti/sound.mp3 ]; then
    echo -e "DOWNLOADING SOUND...\n\n"
    touch ~/noti/sound.mp3 | wget -O ~/noti/sound.mp3 "https://notificationsounds.com/wake-up-tones/rise-and-shine-342/download/mp3"
    sudo echo "alias noti=\"sh ~/noti/noti.sh\"" >> ~/.bashrc
    source ~/.bashrc        
    echo -e "\n\n"
fi

# notify with the sound playing and particular given message and time
echo "notify-send \""$MSSG\"" && play ~/noti/sound.mp3" | at $TIME

如何使用?

首次运行-设置:

  1. 在家里创建一个新目录并调用它 noti

    mkdir ~/noti
    
  2. 下载noti.sh并将其压缩到上述noti目录。

  3. 打开终端并将目录更改为 noti

    cd ~/noti
    
  4. 通过发出以下命令使noti.sh可执行:

    sudo chmod +x noti.sh
    
  5. 运行这样的测试:

    sh ~/noti/noti.sh "Test" "now"
    

例子

noti "Hello From Noti" "now +1 minute"
noti "Hello From Noti" "now +5 minutes"
noti "Hello From Noti" "now + 1 hour"
noti "Hello From Noti" "now + 2 days"
noti "Hello From Noti" "4 PM + 2 days"
noti "Hello From Noti" "now + 3 weeks"
noti "Hello From Noti" "now + 4 months"
noti "Hello From Noti" "4:00 PM"
noti "Hello From Noti" "2:30 AM tomorrow"
noti "Hello From Noti" "2:30 PM Fri"
noti "Hello From Noti" "2:30 PM 25.07.18"

用于通知过程完成(示例)

sudo apt-get update; noti "Done" "now"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.