Comment effacer la notification envoyée par le script shell

Oct 12 2020

Dans mon script, j'envoie une notification à l'utilisateur et je l'invite à saisir des données. Après cela, je veux effacer la notification qui a été envoyée plus tôt. La notification doit rester dans le bac jusqu'à ce que l'utilisateur entre les données.

Actuellement, j'utilise notify-send, mais il ne peut être effacé que par timeout.

Ce que je veux, c'est quelque chose comme ça:

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

J'utilise linux Mint 20. Avec cinnamon DE (v4.6.6).

notify-osd N'est pas installé.

Réponses

2 KamilMaciorowski Oct 13 2020 at 16:36

C'est possible, mais pas avec notify-send. Vous devez parler à votre service de notifications de bureau avec un outil moins spécialisé, comme gdbus. Cela vous permettra d'appeler différentes méthodes depuis un shell.

Une alternative est dbus-send, mais elle ne prend pas en charge (du moins pour le moment) le varianttype requis par la org.freedesktop.Notifications.Notifyméthode. Nous nous en tiendrons gdbus.

Vérifiez man 1 gdbus. Il comprend un exemple qui envoie une 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

Cela devrait imprimer (uint32 42,)42est l'ID de notification. En fait, l'exemple spécifie 42explicitement, ce qui signifie que s'il existe déjà une notification avec cet ID exact, elle sera remplacée. Pour créer une nouvelle notification, spécifiez 0. Un identifiant codé en dur (comme 999dans votre question) est possible, mais que se passe-t-il si l'identifiant est déjà pris par une notification non liée, éventuellement importante? Pour cette raison, spécifiez 0, capturez la sortie, isolez le nouvel ID et utilisez-le avec org.freedesktop.Notifications.CloseNotification.

Liens utiles:

  • org.freedesktop.Notifications.Notify
  • org.freedesktop.Notifications.CloseNotification
  • noms d'icônes standard

Exemple de 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: ' read foo >/dev/null gdbus call --session \ --dest org.freedesktop.Notifications \ --object-path /org/freedesktop/Notifications \ --method org.freedesktop.Notifications.CloseNotification \ "$id"