Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How can I create a transient notification on-screen?

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,714
    Distro
    Lubuntu 18.04 Bionic Beaver

    Question How can I create a transient notification on-screen?

    I can use notify-send to display an on-screen message.

    • With --urgency=low or --urgency=normal, the message disappears from view after 4 seconds, but it remains in the history (when you click the clock at the top) until manually dismissed.
    • With --urgency=critical , the message remains on-screen until manually dismissed.

    There's a third option that notify-send doesn't seem to support. I've seen messages by some apps display on-screen and disappear by themselves, but they don't remain in the history. Once they disappear, they've gone forever. For example, the Gnome extension Lock Keys does exactly this — there's no need to keep a historical record of how often you've pressed Caps Lock.

    I want to do the same thing for certain messages, and I can't figure out how. The option --expire-time is ignored in Gnome, so that doesn't help. There obviously is an alternative on-screen notification command that looks like notify-send but allows for transient messages. What is it?

    I'm using standard Ubuntu 20.04, which has Gnome 3.36.3.

    Thank you
    Last edited by Paddy Landau; December 12th, 2020 at 06:39 PM.

  2. #2
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How can I create a transient notification on-screen?

    I've used the lines as described here:https://askubuntu.com/questions/8684...mes-lockscreen
    I know it works 20.04
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags

  3. #3
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: How can I create a transient notification on-screen?

    You can do it with zenity.

    I don't think you can control the time with --notification, but you can with --info. Try and tweak

    Code:
    zenity --info --text="zenity can do it for you" --no-wrap --timeout=2

  4. #4
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How can I create a transient notification on-screen?

    Quote Originally Posted by sudodus View Post
    You can do it with zenity.

    I don't think you can control the time with --notification, but you can with --info. Try and tweak

    Code:
    zenity --info --text="zenity can do it for you" --no-wrap --timeout=2
    Nice!
    Attached Images Attached Images
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags

  5. #5
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: How can I create a transient notification on-screen?

    Nice to meet you again in this thread, 1fallen

  6. #6
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,714
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: How can I create a transient notification on-screen?

    Quote Originally Posted by 1fallen View Post
    I've used the lines as described here:https://askubuntu.com/questions/8684...mes-lockscreen
    Thanks, but that does absolutely nothing for me! I've copied the script exactly, and when I run it, nothing happens. No error is shown in the terminal.
    Quote Originally Posted by sudodus View Post
    You can do it with zenity…
    Thanks. I'm aware of Zenity, but I was hoping to use the notification method, because it doesn't grab focus from the current task.

    However, your comment on Zenity made me think. I usually use Yad (a fork of Zenity, available in the standard repositories) because it's far more powerful. This is not an ideal solution, but it comes close.
    Code:
    #!/usr/bin/env bash
    
    ####################################################################################################
    # notify-send-transient: Simulate a transient notification.
    # It works like notify-send --urgency=low, but the notification doesn't appear in the history.
    # Simply call with the message that you want to display.
    ####################################################################################################
    
    function initialise
    {
        set -u                                 # Catch uninitialised variables.
        local -ir NOTIFICATION_WIDTH=500       # Desired width of notification.
    
        # Find the screen width.
        local -ir WIDTH=$( xrandr --nograb --current |
                           grep -E --only-matching ' connected primary [[:digit:]]+' |
                           cut --delimiter=' ' --field=4
                         )
    
        # How wide is the dock? I don't know how to find this directly, so I've used the icon width
        # and added 22 (because that's what I measured). It won't be quite correct on other systems.
        local -ir DOCK_ICONS=$( gsettings get org.gnome.shell.extensions.dash-to-dock dash-max-icon-size )
        local -ir DOCK_WIDTH=$(( DOCK_ICONS + 22 ))
    
        # Calculate where to place the left of the notification in order to centre it in the non-dock area.
        declare -gir CENTRE_POINT=$(( ( WIDTH - DOCK_WIDTH ) / 2 + DOCK_WIDTH - ( NOTIFICATION_WIDTH / 2 ) ))
    
    } # initialise
    
    function displayTransientMessage
    {
        local -r MESSAGE="${@}"                # Combine all parameters into a message.
    
        # Display the message: Time out, don't grab focus, show on top, stick on all desktops; send to background.
        yad --text-info -on-top --sticky --no-focus --skip-taskbar --undecorated --width=${NOTIFICATION_WIDTH}   \
             --posx=${CENTRE_POINT} --posy=1 --button='Dismiss!gtk-ok' --buttons-layout=center \
             --wrap --justify=center --fontname=normal --timeout=4 --fore=white --back=black   \
             --listen <<<"${MESSAGE}" &
    
    } # displayTransientMessage
    
    initialise
    displayTransientMessage "${@}"
    And then execute with:
    Code:
    notify-send-transient 'Please read this message before it disappears forever!'
    I'd still like to know the proper way to do this, so my question isn't answered, but this will do as a workaround until I find the correct answer.
    Last edited by Paddy Landau; December 13th, 2020 at 02:29 PM. Reason: Small correction

  7. #7
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How can I create a transient notification on-screen?

    Yup, Gnome adds a new twist to your request. I've been away far to long to have an instant call to memory, so I had forgoten gnome's dos and don'ts.

    This script is the original but the sound dl link is now "404" :Found here: https://gist.github.com/John-Almarde...ad229d7d64.zip
    It also Downloads everything needed

    Code:
    #!/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
    I modified it with my own sound.mp3. (if further guidance is needed let me know)
    I wonder if this will work for you, I use "Mate" here so your mileage may vary.
    • Setting Up:

    • Create a new Directory at your home and call it noti
    • mkdir ~/noti
    • Download noti.sh and extract it to the above noti dir.
    • Open Terminal and Change Directory to noti
    • cd ~/noti
    • Make noti.sh executable by issuing:
    • sudo chmod +x noti.sh
    • Run a Test like this:


    Code:
    sh ~/noti/noti.sh "Test" "now"
    FWIW my (changed) script is as follows due to the broken download links for sounds: (any .mp3 should work just rename it.)
    Code:
    #!/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
    
    fi
    
    # notify with the sound playing and particular given message and time
    echo "notify-send \""$MSSG\"" && play ~/noti/sound.mp3" | at $TIME
    Last edited by 1fallen; December 13th, 2020 at 08:05 PM. Reason: Add link of source
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags

  8. #8
    Join Date
    Jan 2017
    Beans
    195

    Re: How can I create a transient notification on-screen?

    First, thanks for mentioning the "Lock Keys" extension. I was not aware of it and as you stated, it is nicely done.

    It looks like most of the gnome-shell extensions use "gjs" which is the javascript bindings for the Gnome libraries. This piqued my interest because I have programmed Gtk/Gdk in C and also have done "some" javascript programming.

    Anyway I came up with the following code that pops up a Gtk window displaying a message that stays on the screen for 5 secs. Notice that it can be styled using css. Just save the code somewhere and make the file executable. Of course the gjs package needs to be installed.

    I don't know if this is anything like what you are looking for but hopefully it will be of some use.
    Code:
    #!/usr/bin/gjs
    
    const Gio = imports.gi.Gio;
    const Gtk = imports.gi.Gtk;
    const GLib = imports.gi.GLib;
    
    class MessagePopup {
        constructor() {
                this.application = new Gtk.Application({
                    application_id: 'org.example.popup',
                 });
                 this.application.connect('activate', this._onActivate.bind(this));
                 this.application.connect('startup', this._onStartup.bind(this));
        }
    
        _onActivate() {
            this._window.present();
        }
    
        _onStartup() {
            this._createWidgets();
        }
    
        _createWidgets() {
            this._window = new Gtk.Window({
                application: this.application,
                window_position: Gtk.WindowPosition.CENTER,
                skip_taskbar_hint: true,   // don't show in taskbar
                default_height: 25,
                default_width: 500
            });
            
            var msg;
         
            if( !ARGV[0] ) {
                msg='Usage: executable "message enclosed in quotes"';
            }
            else {
                msg= ARGV[0];
            }
            
            this.label = new Gtk.Label({
                label: msg 
            });
            
            var css = new Gtk.CssProvider();
            css.load_from_data(" * { font-size: 16px; font-weight: bold; color: white; background-color: black }");
            this.label.get_style_context().add_provider(css, 0);
            this._window.add (this.label);
            this._window.set_decorated(false);   // no title bar
            this._window.show_all();
            GLib.timeout_add( 0, 5000, () => this.application.quit() );  // close after 5 sec.
        }
    };
    
    let app = new MessagePopup();
    app.application.run (ARGV);
    https://packages.ubuntu.com/focal/gjs
    https://gitlab.gnome.org/GNOME/gjs/-...ster/README.md

  9. #9
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,714
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: How can I create a transient notification on-screen?

    Quote Originally Posted by 1fallen View Post
    Yup, Gnome adds a new twist to your request…
    Thanks for your script, @1fallen, but that still uses notify-send, which is precisely what I'm trying to avoid!
    Quote Originally Posted by norobro View Post
    First, thanks for mentioning the "Lock Keys" extension. I was not aware of it and as you stated, it is nicely done.
    Did you notice that you can choose between two different types of notification? OSD goes in the middle of the screen, and Compact looks exactly like the standard Gnome notification. The latter is what I'm really after!
    Quote Originally Posted by norobro View Post
    It looks like most of the gnome-shell extensions use "gjs"…
    It seems that Ubuntu already installs gjs by default, because I already have it.

    Thank you for your script. Although it's not quite the same look-and-feel, it does work!

    There are two changes that I'd like, if at all possible.

    1. I've changed the colours and font to match my theme, but I don't know how to use the CSS to size the message (size 500×70 px) or to position it (the left point (X,Y) should be (737,1)). Do you know how I can do this?
    2. It grabs the focus away from the current window. Is it possible to prevent that, which would make it just like the Gnome notification?

    Thank you again!

  10. #10
    Join Date
    Jan 2017
    Beans
    195

    Re: How can I create a transient notification on-screen?

    You're welcome.

    See the comments in red:
    Code:
       _createWidgets() {
            this._window = new Gtk.Window({
                application: this.application,
                window_position: Gtk.WindowPosition.CENTER,
                skip_taskbar_hint: true,   // don't show in taskbar
                default_height: 25,          // change to 70
                accept_focus: false,       // added so window doesn't grab focus
                default_width: 500
            });
            this._window.move( 737, 1);  // added to relocate window before it is displayed
            var msg;
            ...
    I don't think you can modify the height/width of a Gtk widget using css.

    We should be able to detect the screen size so the popup will be centered on different sized screens. I'll look into it and post back if I figure it out.

    Gnome shell provides a lot of javascript extensions that can be imported. Many are used in extensions like 'Lock Keys', but I'm not familiar enough with gjs to use them. For example here is a link to modules for user interface: https://gitlab.gnome.org/GNOME/gnome...e/master/js/ui

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •