Update Notification Using KNotify

I converted the gnome python script to use KDE notifications every time Spotify changes track. Code below:

#!/usr/bin/env python # -*- coding: utf-8 -*- """ Title: Spotify Notification Demo Author: Stuart Colville, http://muffinresearch.co.uk Modified to work with KDE: Steve Nixon License: BSD """ import dbus import gobject from dbus.mainloop.glib import DBusGMainLoop class SpotifyNotifier(object): def __init__(self): """initialise.""" bus_loop = DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus(mainloop=bus_loop) loop = gobject.MainLoop() self.spotify = bus.get_object("org.mpris.spotify", "/") self.spotify.connect_to_signal("TrackChange", self.track_changed) self.notify_id = None loop.run() def track_changed(self, metadata): """Handle track changes.""" if metadata: title = unicode(metadata.get("title").encode("latin-1"), "utf-8") album = unicode(metadata.get("album").encode("latin-1"), "utf-8") artist = unicode(metadata.get("artist").encode("latin-1"), "utf-8") knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify") knotify.event("warning", "kde", [], title, u"by %s from %s" % (artist, album), [], [], 0, 0, dbus_interface="org.kde.KNotify") if __name__ == "__main__": SpotifyNotifier() 

This is great for every notification, but leaves a record in the KNotify area indefinitely.

Can i either

a) update the event using id and knotify.update? I think so, but I do not know how to verify that the initial existence of an event uses knotify.event for the first time and knotify.update for each subsequent one.

b) Close the event after 30 seconds (or so)

c) Close the event at the end of the song (better than option b), but comes with the same issue of tracking identifiers that I did not develop how to do this)

Cheers for any help,

Oh, also, if someone knows how to stop him by making a little β€œpiano” noise when he notifies that it's great too!

Steve

+4
source share
2 answers

event method returns int. This notification identifier. So you can use it to close, update an existing notification:

 id = knotify.event("warning", ....) time.sleep(30) knotify.closeNotification(id) 

If you need to check if it exists, you can write Id to tmp file, etc.

+1
source

It would be easier to just set a timeout when creating a notification event:

 knotify.event(event, fromApp, contexts, title, text, pixmap, actions, timeout, winId) 

timeout is an integer value representing milliseconds.

+2
source

All Articles