pythonnotificationsgtk

Why is Gio.Notification not showing in Python?


I would like to show notifications using my Gtk app, but when I run my code below, I everything works but the notification doesn't show, even when I click on the button. I tried running it using a desktop file like the one suggested in this answer, but it still didn't work. Here's my code:

import gi
import sys

gi.require_version("Gtk", "3.0")
from gi.repository import Gio, Gtk

class App(Gtk.Application):
    
    def __init__(self, *args, **kwargs):
        Gtk.Application.__init__(self, *args, application_id="org.example.myapp", **kwargs)
        self.window = None

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        if not self.window:
            self.button = Gtk.Button(label="send notification")
            self.button.connect("clicked", self.notnotnot)
            self.window = Gtk.ApplicationWindow(application=self)
            self.window.add(self.button)
            self.window.show_all()
            self.window.present()

    def notnotnot(self, *args):
        notification = Gio.Notification()
        notification.set_body("Hello!")
        self.send_notification(None, notification)

if __name__ == "__main__":
    app = App()
    app.run(sys.argv)

and here's the desktop file org.example.myapp.desktop:

[Desktop Entry]
Type=Application
Name=My Application
Exec=python3 /home/user/programs/python/testing/SO/problem_why_is_gtk....py
Terminal=true
X-GNOME-UsesNotifications=true

Solution

  • I figured out that just setting the priority to high made the notification appear. Note that Gio.Notification.set_urgent() is deprecated. You need to use Gio.Notification.set_priority(). Here is the code with the added line marked accordingly:

    import gi
    import sys
    
    gi.require_version("Gtk", "3.0")
    from gi.repository import Gio, Gtk
    
    class App(Gtk.Application):
        
        def __init__(self, *args, **kwargs):
            Gtk.Application.__init__(self, *args, application_id="org.example.myapp", **kwargs)
            self.window = None
    
        def do_startup(self):
            Gtk.Application.do_startup(self)
    
        def do_activate(self):
            if not self.window:
                self.button = Gtk.Button(label="send notification")
                self.button.connect("clicked", self.notnotnot)
                self.window = Gtk.ApplicationWindow(application=self)
                self.window.add(self.button)
                self.window.show_all()
                self.window.present()
    
        def notnotnot(self, *args):
            notification = Gio.Notification()
            notification.set_body("Hello!")
            notification.set_priority(Gio.NotificationPriority.HIGH) ### ADDED LINE
            self.send_notification(None, notification)
    
    if __name__ == "__main__":
        app = App()
        app.run(sys.argv)
    

    If you specify a string id instead of None when sending the notification (self.send_notification("my_notif_id", notification)), you can later withdraw the notification using self.withdraw_notification("my_notif_id").