Ubuntu桌面通知

前端之家收集整理的这篇文章主要介绍了Ubuntu桌面通知前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

软硬件环境

  • ubuntu
  • notify-osd
  • libnotify-bin

概述

D-Bus是一种消息总线系统,它为应用程序之间互相调用提供了更为简单的方法。本文中实现的桌面通知就是基于D-Bus机制。桌面通知在ubuntu系统的早期版本中就有应用,如常见的音量调节、thunderbird的来信通知、QQ消息通知等都是如此。

先给一张系统架构图,方便理解。

为简单起见,在这里称请求方为Client,响应请求的为Server。

Server

  1. sudo apt-get source notify-osd
  2. cd notify-osd-0.9.24
  3. vi notify-osd/src/default.c
  4.  
  5. #define DEFAULT_DESKTOP_BOTTOM_GAP 6.0f
  6. #define DEFAULT_BUBBLE_WIDTH 24.0f
  7. #define DEFAULT_BUBBLE_MIN_HEIGHT 5.0f
  8. #define DEFAULT_BUBBLE_MAX_HEIGHT 12.2f
  9. #define DEFAULT_BUBBLE_VERT_GAP 0.5f
  10. #define DEFAULT_BUBBLE_HORZ_GAP 0.5f
  11. #define DEFAULT_BUBBLE_SHADOW_SIZE 0.7f
  12. #define DEFAULT_BUBBLE_SHADOW_COLOR "#000000"
  13. #define DEFAULT_BUBBLE_BG_COLOR "#131313"
  14. #define DEFAULT_BUBBLE_BG_OPACITY "#cc"
  15. #define DEFAULT_BUBBLE_HOVER_OPACITY "#66"
  16. #define DEFAULT_BUBBLE_CORNER_RADIUS 0.375f
  17. #define DEFAULT_CONTENT_SHADOW_SIZE 0.125f
  18. #define DEFAULT_CONTENT_SHADOW_COLOR "#000000"
  19. #define DEFAULT_MARGIN_SIZE 1.0f
  20. #define DEFAULT_ICON_SIZE 3.0f
  21. #define DEFAULT_GAUGE_SIZE 0.625f
  22. #define DEFAULT_GAUGE_OUTLINE_WIDTH 0.125f
  23. #define DEFAULT_TEXT_FONT_FACE "Sans"
  24. #define DEFAULT_TEXT_TITLE_COLOR "#ffffff"
  25. #define DEFAULT_TEXT_TITLE_WEIGHT TEXT_WEIGHT_BOLD
  26. #define DEFAULT_TEXT_TITLE_SIZE 1.0f
  27. #define DEFAULT_TEXT_BODY_COLOR "#eaeaea"
  28. #define DEFAULT_TEXT_BODY_WEIGHT TEXT_WEIGHT_NORMAL
  29. #define DEFAULT_TEXT_BODY_SIZE 0.9f
  30. #define DEFAULT_PIXELS_PER_EM 10.0f
  31. #define DEFAULT_SYSTEM_FONT_SIZE 10.0f
  32. #define DEFAULT_SCREEN_DPI 96.0f
  33. #define DEFAULT_GRAVITY GRAVITY_NORTH_EAST

Note

  1. DEFAULT_GRAVITY可以取如下3个值
  2. * GRAVITY_NONE: the behavIoUr in Jaunty. A notification will always go into the top right corner.
  3.  
  4. * GRAVITY_EAST: the behavIoUr NotifyOsd has been having for about a week in Karmic's development cycle,with the notifications centered vertically on the screen.
  5.  
  6. * GRAVITY_NORTH_EAST: the Karmic behavIoUr. Works like GRAVITY_NONE but does not put asynchronous notifications on top of the screen,even if there is no synchronous notification being displayed.
  7.  
  8. ./configure
  9. make
  10. make install(if you need)

Client

  1. sudo apt-get install libnotify-bin

Have a test

  1. 启动server

    notify-osd/src/notify-osd

  2. 修改文字显示大小
    将 DEFAULT_TEXT_TITLE_SIZE 改为4.0f

  3. Client发送请求

    notify-send “djstava” “I Love It”

桌面通知的python实现

  1. 安装pynotify模块
    http://www.galago-project.org/downloads.php下载notify-python-0.1.1.tar.gz,解压然后进行安装三部曲./configure、make、sudo make install

  2. 模块的使用

    try:
    import pynotify
    if pynotify.init(‘Fist Test’):
    n = pynotify.Notification(‘Title’,’message’)
    n.show()
    else:
    print ‘Pynotify module not initialized’
    except:
    print ‘Pynotify module not installed’

    可以像下面一样来设置消息的紧急级别

    1. n.set_urgency(pynotify.URGENCY_LOW)
    2. n.set_urgency(pynotify.URGENCY_NORMAL)
    3. n.set_urgency(pynotify.URGENCY_CRITICAL)

    关闭消息通知操作

    1. n.close()

参考资料

1、http://kobesearch.cpan.org/htdocs/Desktop-Notify/Desktop/Notify.pm.html
2、http://www.galago-project.org/specs/notification/0.9/index.html
3、https://wiki.ubuntu.com/NotifyOSD
4、http://doc.trolltech.com/4.2/intro-to-dbus.html
5、http://www.freedesktop.org/wiki/Software/dbus

猜你在找的Ubuntu相关文章