如何设置基于计时器的通知?

如何设置基于计时器的通知?

作为一名电脑迷,我整天都坐在电脑前。有时,当我在电脑前工作时,我会忘记时间。我需要一个通知服务来提醒我当前时间,无论是通过弹出通知还是播放声音,或者两者兼而有之。

对于弹出窗口,我发现免费桌面通知标准使用DBus 接口

我能够使用创建通知脚部,一个图形化的 DBUS 浏览器。我使用了以下参数:

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

到目前为止它运行良好,但我怎样才能进一步实现它呢?

  • 我如何从命令行调用它?
  • 如何自动执行此命令?这cron仍然是推荐的基于时间的操作自动化方法吗?
  • 如何播放弹出窗口时的声音?通过 FreeDesktop API 还是通过媒体播放器?

完整的解决方案将受到赞赏,并且也许对其他人也有用。

答案1

由于无法使用,dbus-send我改用了 Python 脚本。pynotify 模块内部使用了 API dbus。为了增加趣味,我在消息中添加了幸运饼干。效果非常好:

#!/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

然后我使用 安排了此时间croncrontab条目如下所示:

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

更新:添加了使用脉冲音频播放声音的方法

答案2

您可以使用一个简单的 Python 脚本,如下所示:

#!/usr/bin/python
import dbus
import sys

bus = dbus.SessionBus()

notify = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
method = notify.get_dbus_method('Notify', 'org.freedesktop.Notifications')

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

答案3

您可以使用 dbus-send 命令发送消息。请参阅手册:dbus-send了解更多详细信息。

相关内容