Linux Notifications (e.g. like Windows tray balloons)

Just starting out? Need help? Post your questions and find answers here.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Linux Notifications (e.g. like Windows tray balloons)

Post by Opcode »

I am wondering if there's an easy way (that doesn't require a ton of code) to display notifications on Linux. A prime example would be Ubuntu has "toast-like" notifications that are usually found on Windows 8 applications. Is there an easy (simple) way of triggering notifications that will work across distributions? It doesn't need to work across all of them although Ubuntu and its derivatives and maybe distributions like Arch. I'm using the following code for throwing balloon notifications on Windows and am curious if the same can be done on Linux with a similar amount of code.

Modified for example.

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  
  Structure NOTIFYICONDATA_v6
    cbSize.l
    hWnd.l
    uID.l
    uFlags.l
    uCallbackMessage.l
    hIcon.l
    szTip.s{128}
    dwState.l
    dwStateMask.l
    szInfo.s{256}
    StructureUnion
      uTimeout.l
      uVersion.l
    EndStructureUnion
    szInfoTitle.s{64}
    dwInfoFlags.l
    guidItem.GUID
    hBalloonIcon.l
  EndStructure
  
  Procedure.i WindowShowBalloonTip(Window.i, TrayIcon.i, Title.s, Message.s, Timeout.l = 3000)
    
    Protected Result.i = #False, Balloon.NOTIFYICONDATA_v6
    
    If OSVersion() >= #PB_OS_Windows_2000
      If IsSysTrayIcon(TrayIcon)
        Balloon\hWnd        = WindowID(Window)
        Balloon\uID         = TrayIcon
        Balloon\uFlags      = #NIF_INFO
        Balloon\dwState     = #NIS_SHAREDICON
        Balloon\szInfoTitle = Title
        Balloon\szInfo      = Message
        Balloon\uTimeout    = Timeout
        Balloon\dwInfoFlags = #NIIF_NOSOUND | #NIIF_INFO
        Balloon\cbSize      = SizeOf(NOTIFYICONDATA_v6)
        Result = Shell_NotifyIcon_(#NIM_MODIFY, Balloon)
      EndIf
    EndIf
    
    ProcedureReturn Result
    
  EndProcedure
  
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  
  ; Linux variant
  
CompilerEndIf
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Linux Notifications (e.g. like Windows tray balloons)

Post by ts-soft »

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Linux Notifications (e.g. like Windows tray balloons)

Post by Oma »

Hi,

for Linux you could try this short one:
(needs libnotify-bin)

Code: Select all

;Options:
;--urgency=low, --urgency=normal, --urgency=critical
;--expire-time=ms
;--icon=StockIcon-Name OR IconFilename
;  with --urgency=critical  you have to click for hiding (on most systems)

Global nSummary.s= "My Application"
Global nBody.s   = "A Desktop-Notification with 'notify-send'" + "\n" + "and a 2. line"
Global nIcon.s   = "--icon=dialog-information --urgency=normal --expire-time=5000"

If Not RunProgram("notify-send", #DQUOTE$ + nSummary  + #DQUOTE$ + " " + #DQUOTE$ + nBody + #DQUOTE$ + nIcon, "")
	Debug "'notify-send' not available!"
	Debug "install: sudo apt-get install libnotify-bin"
EndIf
Cheers, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Post Reply