#StackBounty: #python #notifications #gtk Why is Gio.Notification not showing in Python?

Bounty: 50

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. 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):
        notification = Gio.Notification()
        notification.set_body("Hello!")
        self.send_notification(None, notification)
        if not self.window:
            self.window = Gtk.ApplicationWindow(application=self)
            self.window.present()

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=false
X-GNOME-UsesNotifications=true


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.