How to send notifications in linux using dunstify/notify-send

In today’s fast-paced world, staying informed is crucial, and desktop notifications have become an integral part of our digital lives. Whether you’re monitoring system statuses, receiving alerts, or simply staying up to date, having a versatile and customizable notification system is essential. This is where dunstify/notify-send comes into play. Dunstify is a command-line utility that lets you send and manage desktop notifications with ease. In this guide, we’ll dive into various aspects of Dunstify to help you harness its power effectively.

Before you begin

dunstify is installed with the application dunst. To learn about how to install it, refer to this article on dunst. And also note that, both the commands dunstify and notify-send are the same. They are symlinked in your linux system.

Dunstify/notify-send configuration

In this article, I have talked in detail about the dunst’s configuration – colors, paddings, frame, timeout, progressbar, separator, font, markup (bold, underline, etc.), text alignment, icon alignment, icons, mouse settings, etc.

Or, if you don’t have the time to configure your dunst, just copy my dunst’s configuration given in the same article here.

Basic dunstify/notify-send command

Let’s start with the basics. Sending a simple notification using Dunstify is remarkably straightforward:

dunstify SmartTech101 Hello

This command triggers a notification with the title “SmartTech101” and the message “Hello”.

Basic dunstify/notify-send command

Closing notifications using shortcuts

For this, you need to bind some shortcut keys in your system. In my i3-tiling window manager, I have bound the commands dunstctl close-all and dunstctl close in the following ways*.

bindsym ctrl+space exec dunstctl close
bindsym ctrl+shift+space exec dunstctl close-all

Pressing the shortcut ctrl+space closes the latest notification (one at a time). If there are more than one notification visible on your screen, you can press ctrl+shift+space to close all the notifications at once.

You can also use mouse buttons to close notifications. By default, clicking on a notification closes it and the right click closes all the notifications.

To learn about how to bind keys in i3, look at this article.

For something other than i3, use your system’s configuration, or use sxhkd, or follow this article to bind any shortcut key with any command.

Urgency Levels in dunstify/notify-send

Notifications aren’t one-size-fits-all; some require immediate attention. With Dunstify, you can set different urgency levels for notifications – critical, normal, low.

notify-send -u critical 'battery < 15%'

Here, we’re using notify-send to send a critical-level notification when the battery drops below 15%.

Urgency Levels in dunstify/notify-send

Adding Icons to Notifications

Icons add visual context to notifications. Dunstify allows you to include icons in your notifications, making them more informative:

notify-send --icon=audio-speakers "Bluetooth Speaker" "Battery < 20%"

In this example, we’re attaching an icon to a notification about the battery being below 20%.

Adding Icons to Notifications

Note: Please make sure that you have configured your dunst properly. I am using Papirus Icon Theme which is a very big collection of icons. You can search for the icons in the directory /usr/share/icons using the following command. And also you need to remove the file extension. For the above example, the file name is /usr/share/icons/Papirus/48x48/devices/audio-speakers.svg. So, use only the name audio-speakers in the --icon.

find /usr/share/icons/ -type f | fzf

Applying Timeouts to Notifications

Sometimes, you want notifications to disappear after a certain period. Dunstify/notify-send lets you achieve this:

dunstify --timeout=500 "Error Finding" "Xdg-open worked."

Here, the notification about the successful execution of xdg-open will vanish after 500 milliseconds.

Progressbar in Notification

Progressbar in the notification can be useful for creating custom volume and brightness notifications. To create progressbar, use the argument --hints=int:value:<progress> in dunstify/notify-send.

For example,

dunstify  --hints=int:value:"50%" "SmartTech101" "Progressbar"

The above command creates a progress bar which has progress half of its total length.

Progressbar in Notification

Replacing Older Notifications in dunstify/notify-send

You can also avoid notification clutter by replacing older ones with updated information. For this, first, you need to create a notification with a specific id using –replace=<id> an argument. For <id>, you can use any random number. For example, the following command creates a notification with an id of 11111.

dunstify --replace=11111 --icon=display-brightness --hints=int:value:"50%" "SmartTech101's Brightness" "Changed"

Now, when you execute any dunstify-command again with the same id, the above notification will be replaced. Try the above with the following command quickly and you would see the result in action.

dunstify --replace=11111 --icon=display-brightness --hints=int:value:"70%" "SmartTech101's Brightness" "Changed"

With this command, the notification about brightness changes is updated while retaining the same ID to replace the previous one.

This argument --replace=<id> is quite useful in creating brightness-change or volume change notifications. In Windows, this is already built-in, but in a minimalistic system like i3 tiling window manager, you can use this feature to create your own volume and brightness change scripts.

Closing Targeted Notifications

Dunstify enables you to close specific notifications programmatically. For this, first, you need to retrieve the id of the notification, using the flag --printid. Then you can close it, using --close flag.

Example:

id="$(notify-send --printid --urgency=critical "Aur+Pacman" "Downloading...")"
yay -Qu 2> /dev/null 1> "$package_list"
notify-send --close="$id"

Here, a critical-level notification is closed using the retrieved ID after updating the package list.

I use this functionality quite often. I get panicked whenever there is internet consumption in my system by background process without me knowing which process is consuming it. So, I have put these notification commands to let me know when pacman/yay is trying to update its database. Similarly, I also use this when my system is backing up (to learn more about this using rsync, look over here).

Taking Actions on notifications using dunstify/notify-send

Dunstify isn’t limited to static notifications; you can interact with them too. Here is a script template:

action="$(dunstify --action="option_1, Menu Item 1" --action="option_2, Menu Item 2" --action="option_3, Menu Item 3" SmartTech101 Hello)"

case "$action" in
"option_1")
    <Command 1> ;;
"option_2")
    <Command 2> ;;
"option_3")
    <Command 3> ;;
esac

First, you need to specify your actions using the flag --action. You can use the flag as many times as you want. All the menu items will be listed in the dmenu. Now, using dmenu, you can select the action, you want. To learn about the dmenu, look at this article.

Now, let’s say you choose the first action. In that case, the value of the variable will be option_1.

Then, the script uses the “case” conditional statement to execute the command_1. To learn about the case conditional statement, head over to this page.

The following example showcases actions you can take on a screenshot notification, from opening the image to editing it using Gimp:

action="$(dunstify --action="open_image,Open the image." --action="open,Open the directory." --action="delete,Delete it." --action="edit,Gimp it" -i "$filename" "Screenshot" "Saved & Copied.")"

case "$action" in
"open")
    "$TERMINAL" -e sh -c "ranger --selectfile=\"$filename\"" ;;
"open_image")
    setsid --fork xdg-open "$filename" ;;
"delete")
    rm "$filename" ;;
"edit")
    setsid --fork gimp "$filename" ;;
esac

You can add other actions as well like reducing the depth of the image and putting a watermark. Here is how it looks:

Taking Actions with Dunstify/notify-send

The symbol(A) in the notification shows that there are actions. Now, you need to execute the command dunstctl context (by using some shortcut key) to open the dmenu. In my i3 tiling window manager, I have bound the command to ctrl+ greater using bindsym ctrl+greater exec dunstctl context. So, I have to press, ctrl+greater as soon as I see the above notification and that launches a dmenu.

Taking Actions with Dunstify/notify-send

Sending Notifications from Cron/Anacron

Automation is a key part of any efficient system. You can use Dunstify to send notifications from cron*/anacron# jobs. However, when you send the notification from cron/anacron, you need to supply the DISPLAY, and DBUS_SESSION_BUS_ADDRESS variables. You might also need to run the notification command as your user not as root user.

For this, find these variables using the echo command:

[ajay@legion .my_scripts]$ echo "$DISPLAY"
:0

[ajay@legion .my_scripts]$ echo "$DBUS_SESSION_BUS_ADDRESS"
unix:path=/run/user/1000/bus

For example, In this command, we’re sending a critical-level notification about a backup operation initiated by Rsync.

sudo -u ajay DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus dunstify -i folder-backup -u critical "Rsync:" "backup started"

In the above command, sudo -u ajay make sure that the upcoming command is to be executed as the username ajay.

*To learn about cron jobs, look at this article.

#To learn about anacron jobs, look at this article.

Some beautiful applications of notifications using dunstify/notify-send

Notify the user if a command exits due to some reasons in unwanted fashions

Put the trap command at the beginning of your script. Use INT, TERM, and ERR so that whenever there is any kind of error/interruption/termination, a notification will be sent.

trap 'dunstify --urgency=critical "mpv.sh: ERROR" "$(printf "%s\n" "Make sure mpv is opened." "Or reopen mpv.")"' INT TERM ERR

Waiting for some applications to close

In Windows, when we try to shut down our PC, the PC does not shut down until all the open applications are closed. But in Linux, when we try to shut it down using the command systemctl poweroff or something similar, it does not wait.

Using the following conditional statement, we can know when our critical application has closed so that we can proceed with the shutdown. It sends a dunstify notification after the confirmation.

while true
do
  pgrep --full /usr/bin/anki > /dev/null || break
  sleep 1s
done
pgrep --full /usr/bin/anki > /dev/null || dunstify "Anki:" "closed"

This code segment employs a continuous loop* to monitor the existence of the Anki process by using ‘pgrep’ to search for the full path of the Anki application. If the process is not found/closed, the loop is broken and you get a notification.

*To learn about the while loop look over here.

Tabulated data in the notification

You can also use dunstify/notify-send to send tabulated data. For this, first, configure your dunst to use some monospace font. For example, the following command outputs tabulated data.

[ajay@legion ~]$ ps axch -o cmd:15,%mem --sort=-%mem | head -n 30

Now, I use that command’s output in dunstify:

notify-send "Memory Consumption (%):" "$(ps axch -o cmd:15,%mem --sort=-%mem | head -n 30)" ;;
Tabulated data in the notification

I also use dunstify/notify-send notifications for weather download failure, mounting USB drive, stopwatch, capslock on and off,

Conclusion

Dunstify/notify-send empowers you to manage desktop notifications with precision and flexibility. From basic notifications to interactive actions, you have a wide array of options to enhance your notification experience. So, whether you’re a power user or just looking to streamline your workflow, Dunstify is a valuable tool to master.

That’s all folks. If you want me to talk more about these scripts/commands in detail ask me in the comment section below. You can put any suggestions in the comment section. Thanks again.

3 thoughts on “How to send notifications in linux using dunstify/notify-send”

  1. Pingback: Backup and Restore Your Computer Using Rsync | SmartTech101

  2. Pingback: How to Configure Dunst Notifications in Linux (with images) | SmartTech101

  3. Pingback: Cron: the Job Scheduler in Linux/Unix | SmartTech101

Leave a Comment

Your email address will not be published. Required fields are marked *