Ubuntu – “don’t disturb” option to temporarily hide notifications, like on macbooks

indicatornotificationnotify-osdnotify-sendscripts

Does there exist "Don't disturb mode" as is does for OSX device, where you can decided when notification can bother you, or not..

I just installed chrome, and I usually get spammed by group texts and other notification, which can be annoying when I try work.. On my macbook I have the option to turn on "Do no disturb" which mutes all form of notication.. Does something like that exist for Ubuntu?

Best Answer

1. Major update

Just finished a completely rewritten version of the indicator (0.9.0). Options now include:

  • suppressing only notifications, containing specific strings
  • suppressing (muting) sound
  • logging missed notifications
  • running on startup
  • remembering the last state (suppressing or not) on next run

Furthermore many, many improvements on interface and behaviour.

enter image description here enter image description here

Installing is unchanged (ppa):

sudo apt-add-repository  ppa:vlijm/nonotifs
sudo apt-get update
sudo apt-get install nonotifs

2. Old(er) answer

Indicator to mute/show notifications

With the indicator below, you can choose to temporarily switch off notifications:

enter image description here

or show notifications:

enter image description here

How it works

The trick is a simple command, using dbus-monitor to intercept upcoming notifications and stop them before they appear.
The indicator is a user-friendly "wrapper" to toggle it off and on.

How to set up


As per now (for Trusty, Vivid, Wily, Xenial):

sudo apt-add-repository  ppa:vlijm/nonotifs
sudo apt-get update
sudo apt-get install nonotifs

This will install globally (including the launcher). Installing via ppa is preferred, since it maintains the latest version, and is regularly updated.
The indicator will appear in Dash as NoNotifications

If you install by the ppa, but previously installed manually from below, please run rm ~/.local/share/applications/nonotif.desktop first to remove the local .desktop file.


Or manually:

The solution exists of a number of items you simply need to store together in one and the same directory.

  1. Create a directory or folder (can be anywhere in your home directory e.g.)
  2. The indicator: Copy the script below into an empty file, save it as nonotif_indicator.py:

    #!/usr/bin/env python3
    import os
    import signal
    import gi
    import subprocess
    gi.require_version('Gtk', '3.0')
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import Gtk, AppIndicator3
    
    currpath = os.path.dirname(os.path.realpath(__file__))
    proc = "nonotifs.sh"
    
    def run(path):
        try: 
            pid = subprocess.check_output(["pgrep", "-f", proc]).decode("utf-8").strip()
        except subprocess.CalledProcessError:
            subprocess.Popen(path+"/"+proc)
    
    def show():
        try:
            pid = subprocess.check_output(["pgrep", "-f", proc]).decode("utf-8").strip()
            subprocess.Popen(["pkill", "-P", pid])
        except subprocess.CalledProcessError:
            pass
    
    class Indicator():
        def __init__(self):
            self.app = 'nonotif'
            iconpath = currpath+"/grey.png"
            self.testindicator = AppIndicator3.Indicator.new(
                self.app, iconpath,
                AppIndicator3.IndicatorCategory.OTHER)
            self.testindicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
            self.testindicator.set_menu(self.create_menu())
    
        def create_menu(self):
            menu = Gtk.Menu()
            item_quit = Gtk.MenuItem('Quit')
            item_quit.connect('activate', self.stop)
            item_silent = Gtk.MenuItem("Don't disturb")
            item_silent.connect('activate', self.silent)
            item_show = Gtk.MenuItem("Show notifications")
            item_show.connect('activate', self.show)
            menu.append(item_quit)
            menu.append(item_silent)
            menu.append(item_show)
            menu.show_all()
            return menu
    
        def stop(self, source):
            Gtk.main_quit()
    
        def silent(self, source):
            self.testindicator.set_icon(currpath+"/red.png")
            run(currpath)
    
        def show(self, source):
            self.testindicator.set_icon(currpath+"/green.png")
            show()
    
    Indicator()
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    Gtk.main()
    
  3. The dbus-monitor script; save it (exactly) as nonotifs.sh in one and the same directory as the first script:

    #!/bin/bash
    dbus-monitor "interface='org.freedesktop.Notifications'" | xargs -I '{}' pkill notify-osd
    

    Make this script executable

  4. Three icons; right-click on each of them and save them together with the two scripts as (exactly):

    enter image description here <-- green.png

    enter image description here <-- red.png

    enter image description here<-- grey.png

  5. That's it. Now test-run the indicator with the command:

    python3 /path/to/nonotif_indicator.py
    

    and switch notifications on/of

Launcher

In case you'd like a launcher with the indicator:

enter image description here

  • Copy the icon below, save it as nonotificon.png:

    enter image description here

  • Copy the code below into an empty file:

    [Desktop Entry]
    Type=Application
    Name=No Notifications
    Exec=python3 /path/to/nonotif_indicator.py
    Icon=/path/to/nonotificon.png
    Type=Application
    
  • Edit the lines:

    Exec=python3 /path/to/nonotif_indicator.py
    

    and

    Icon=/path/to/nonotificon.png
    

    according to the actual paths, and save the file as nonotif.desktop in ~/.local/share/applications

Add the indicator to Startup Applications

You can add the indicator to Startup Applications: Dash > Startup Applications > Add. Add the command:

/bin/bash -c "sleep 15 && python3 /path/to/nonotif_indicator.py"
Related Question