6

In my script I'm sending notification to the user and prompting him to enter some data. After that I want to clear notification that was sent earlier. The notification should stay in tray until user enters data.

Currently I'm using notify-send, but it could be cleared only by timeout.

What I want is something like that:

send-notification --title="Hey, enter some data pls" --id=999
prompt-to-enter-data
delete-notification --id=999

I'm using linux Mint 20. With cinnamon DE (v4.6.6).

notify-osd is not installed.

1 Answer 1

8

It's possible, but not with notify-send. You need to talk to your desktop notifications service with a less specialized tool, like gdbus. It will allow you to call various methods from a shell.

An alternative is dbus-send, but it doesn't (at least for now) support the variant type needed by the org.freedesktop.Notifications.Notify method. We will stick to gdbus.

Check man 1 gdbus. It includes an example which sends a notification:

gdbus call --session \
            --dest org.freedesktop.Notifications \
            --object-path /org/freedesktop/Notifications \
            --method org.freedesktop.Notifications.Notify \
            my_app_name \
            42 \
            gtk-dialog-info \
            "The Summary" \
            "Here's the body of the notification" \
            [] \
            {} \
            5000

This should print (uint32 42,) where 42 is the notification ID. In fact the example specifies 42 explicitly, which means if there already is a notification with this exact ID then it will be replaced. To create a brand new notification specify 0. A hardcoded ID (like 999 in your question) is possible, but what if the ID is already taken by some unrelated, possibly important notification? For this reason specify 0, capture the output, isolate the new ID and use it with org.freedesktop.Notifications.CloseNotification.

Useful links:

Example script:

#!/bin/sh

tty="$(tty)"
id="$(gdbus call --session \
                 --dest org.freedesktop.Notifications \
                 --object-path /org/freedesktop/Notifications \
                 --method org.freedesktop.Notifications.Notify \
                 my_script \
                 0 \
                 utilities-terminal \
                 "Response required" \
                 "Script awaiting input ($tty)." \
                 [] \
                 {} \
                 0
      )"
id="${id##* }"
id="${id%,)}"
printf 'Type here and hit Enter: '
read foo
>/dev/null gdbus call --session \
                      --dest org.freedesktop.Notifications \
                      --object-path /org/freedesktop/Notifications \
                      --method org.freedesktop.Notifications.CloseNotification \
                      "$id"
6
  • Wow, thanks a lot! I've already lost all hope to get any answer for this question. :)
    – Demiler
    Commented Oct 13, 2020 at 14:42
  • Id = 999 was just an example, it wasn't a requirement. Thanks again!
    – Demiler
    Commented Oct 13, 2020 at 14:49
  • I really want a keyboard shortcut to close all open notifications -- do you know a way to get the ids of open notifications that weren't opened in this way? Looks like it might not be possible.
    – simon
    Commented Mar 15, 2021 at 19:21
  • @simon This is not how the site is supposed to work. Even if I knew a way, I wouldn't publish it here. Ask a new question; include a link to this one if you think it provides context. Then you can lure me by linking to the new question in yet another comment here. I don't know the answer yet but maybe I will research the problem. Or maybe somebody else will help you. Commented Mar 15, 2021 at 19:55
  • Fair enough mate, I was going to suggest I could post a new question, but not to worry.
    – simon
    Commented Mar 15, 2021 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .