Ubuntu – How to setup a timer based notification

dbusnotification

Being a compulsive computer user, I am in front of it all day. I sometimes lose track of time when I am working on my comp. I need a notification service to alert me of the current time, either by a popup notification or a sound being played or both.

For the popup, I found the Free Desktop notification standard which uses a DBus API.

I was able to create a notification using DFeet, a graphical DBUS explorer. I used the following arguments:

"wakeup", 1234, "", "The time is", "9PM", [], [], 1

It works fine so far, but how can I take it further from here?

  • How do I invoke this from the command-line?
  • How do I automate this command? Is cron still the recommended way of automating time based actions?
  • How do I play sounds along with the popup? Either via the FreeDesktop API or via a media player?

A complete solution would be appreciated and perhaps useful to others too.

Best Answer

Since I couldn't use dbus-send I wrote a python script instead. The pynotify module internally uses the dbus API. For extra kicks I added a fortune cookie in the message. Works like a charm:

#!/usr/bin/env python
"""python 2.7 script that creates a notification using pynotify. It shows the current time and a small fortune cookie"""
try:
  import pynotify
  import time
  import subprocess
  if pynotify.init("Wakeup service"):
    subprocess.Popen(["paplay", "/usr/share/sounds/ubuntu/stereo/message.ogg"])

    # You can get more stock icons from here: http://stackoverflow.com/questions/3894763/what-icons-are-available-to-use-when-displaying-a-notification-with-libnotify
    timeStr = time.strftime("%I:%M %p %d %b")
    cookie = subprocess.check_output(["/usr/games/fortune", "-s"])
    n = pynotify.Notification(timeStr, cookie, "/usr/share/app-install/icons/ktimer.png")
    n.set_timeout(1)
    n.show()
  else:
    print "problem initializing the pynotify module"
except Exception as exc:
  print "Exception", exc

I then scheduled this using cron. The crontab entry looks like:

0,30 * * * * DISPLAY=:0 ./local/bin/notify_new.py

Update:Added a method to play a sound using pulse audio

Related Question