38

I wrote a python code for getting random text into a .txt file. Now I want to send this random text into notification area via 'notify-send' command. How do we do that?

63

We can always call notify-send as a subprocess, e.g like that:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import subprocess

def sendmessage(message):
    subprocess.Popen(['notify-send', message])
    return

Alternatively we could also install python-notify and call the notification through that:

import pynotify

def sendmessage(title, message):
    pynotify.init("Test")
    notice = pynotify.Notification(title, message)
    notice.show()
    return

Note that there's no python3-notify package available in Ubuntu. If you're using Python 3, you will need to use python3-notify2. The API for notify2 is the same: just replace pynotify with notify2.

  • 1
    I had to use subprocess.Popen(['notify-send', message]) to make the first example work. – lgarzo Feb 29 '12 at 16:52
  • The second one a pynotify.init("Test") and pynotify.Notification(title, message).show(). By the way I'm „Learning Python The Hard Way”, so I might just overlook something... – lgarzo Feb 29 '12 at 17:07
  • So sorry for that. I was unable to test this earlier on my box at work - my bad. Edited the missing bits in. – Takkat Feb 29 '12 at 17:21
10

python3

Whilst you can call notify-send via os.system or subprocess it is arguably more consistent with GTK3 based programming to use the Notify gobject-introspection class.

A small example will show this in action:

from gi.repository import GObject
from gi.repository import Notify

class MyClass(GObject.Object):
    def __init__(self):

        super(MyClass, self).__init__()
        # lets initialise with the application name
        Notify.init("myapp_name")

    def send_notification(self, title, text, file_path_to_icon=""):

        n = Notify.Notification.new(title, text, file_path_to_icon)
        n.show()

my = MyClass()
my.send_notification("this is a title", "this is some text")
  • Not just more consistent with Gtk programming, but also it's a single process: there's no fork-exec going on, so no waste of resources on spawning new process just to send a notification bubble, plus Popen() will call shell to run the command, so there's shell process popping up too. – Sergiy Kolodyazhnyy Oct 15 '18 at 23:20
6
import os
mstr='Hello'
os.system('notify-send '+mstr)
  • How do I customize this notification? Like heading, image in notification, etc.? – mehulmpt Mar 25 '15 at 18:34
  • 1
    This version is vulnerable to arbitrary command execution. – Steve Oct 16 '18 at 3:05
5

To answer Mehul Mohan question as well as propose the shortest way to push a notification with title and message sections:

import os
os.system('notify-send "TITLE" "MESSAGE"')

Putting this in function might be a bit confusing due to quotes in quotes

import os
def message(title, message):
  os.system('notify-send "'+title+'" "'+message+'"')
  • 4
    You could suggest an edit to that post. – muru Oct 26 '15 at 10:25
  • 2
    What about using 'notify-send "{}" "{}"'.format(title, message) rather than adding strings? – Stam Kaly Jan 31 '17 at 21:22
4

For anyone looking at this in +2018, I can recommend the notify2 package.

This is a pure-python replacement for notify-python, using python-dbus to communicate with the notifications server directly. It’s compatible with Python 2 and 3, and its callbacks can work with Gtk 3 or Qt 4 applications.

3

You should use notify2 package, it is a replacement for python-notify. Use it as followed.

pip install notify2

And the code:

import notify2
notify2.init('app name')
n = notify2.Notification('title', 'message')
n.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.