I have a custom script and I want to send a desktop notification (the one that appears in the top right corner of the screen) with a custom message. How do I do that?
Asked
Active
Viewed 7.8k times
136
-
is there a C library that we can bind to for this? It would be nice to not have to launch an extra child process if possible. – Alexander Mills Mar 21 '20 at 20:54
-
anyone know offhand how to do something similar on MacOS/Macbook pro? – Alexander Mills Mar 21 '20 at 20:54
5 Answers
204
There are a bunch of other cool features with notify-send
We can run a command and make it display in the notification:
notify-send <title> <`command`>
notify-send Date "`date`"
notify-send Disk "`df / -H`"
We can use icons with the notifications
notify-send -i <icon> <Message>
notify-send -i face-wink "Hello! January"
Really annoying pop up
notify-send -t 0 "Bringing down the system"
and
notify-send <title> <message>
notify-send "who am i" "I am January"
For more options check here
-
2Thank you. Where can we get a list of icons, e.g. the `face-wink` that you used? – Paddy Landau Sep 24 '12 at 10:56
-
4check out my answer [here](http://askubuntu.com/questions/189231/where-are-the-stock-icon-names-defined-for-the-unity-panel-service-indicators-an/189262#189262) – devav2 Sep 24 '12 at 13:07
-
`notify-send -t 0` works but `notify-send "who am i" "I am January"` does not work :( - on ubuntu 15.10 – AlikElzin-kilaka May 23 '16 at 09:19
-
5
-
2
-
is there a C library that we can bind to for this? It would be nice to not have to launch an extra child process if possible. – Alexander Mills Mar 21 '20 at 20:52
-
anyone know offhand how to do something similar on MacOS/Macbook pro? – Alexander Mills Mar 21 '20 at 20:54
20
Just to add to the other answers, when running the command locally from cron, I use
DISPLAY=:0.0 /usr/bin/notify-send "TITLE" "MESSAGE"
CelticRaven
- 321
- 1
- 3
-
This may fail if you currently used display appears to be at `:1` or something else. I've noticed such things happen when e.g. Xorg crashes and gets restarted. – Ruslan Apr 29 '20 at 12:00
-
To determine which display is used run echo $DISPLAY in console... – Aleksandar Pavić Apr 21 '22 at 19:50
18
I stumbled upon that one by chance. Answer: use the program notify-send:
notify-send "Hello world!"
Gilles 'SO- stop being evil'
- 59,745
- 16
- 131
- 158
January
- 35,223
- 15
- 82
- 101
6
I created a simple and almost-native script that plays Sound and displays a Notification with a Given Message and Time for 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
How To Use?
First Run - Setting Up:
Create a new Directory at your home and call it
notimkdir ~/notiDownload noti.sh and extract it to the above
notidir.Open Terminal and Change Directory to
noticd ~/notiMake noti.sh executable by issuing:
sudo chmod +x noti.shRun a Test like this:
sh ~/noti/noti.sh "Test" "now"
Examples
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"
For Notifying The Finish of Process (example)
sudo apt-get update; noti "Done" "now"
Yahya
- 171
- 1
- 5
4
-
(Late, I know, but still valid) I disagree that this is an alternative, as `zenity` displays dialog boxes that don't go away by themselves. – noughtnaut Nov 16 '20 at 20:31
-
1
-
@Noughtnaut on the other hand notify-send's `-t` option is ignored by Ubuntu's Notify OSD and GNOME Shell. I could not make it work even in Xfce. (See the man page of it.) – jarno Mar 29 '22 at 10:28