Grouping desktop notifications with libnotify and notify-send

In writing a script which generated libnotify desktop notifications with notify-send, I saw that they soon piled up and cluttered the notification area. Some applications (such as Transmission, or ABRT) are able to stack/group their notifications together, so I set out to discover how.

Screenshot showing grouped and ungrouped notifications

After a couple of hours of googling, reading and asking in irc channels I discovered it’s a lot more complicated than I hoped, and that no one has really written it down clearly anywhere. So, for the benefit of anyone like me who searches for a solution, here is what I found.

Firstly, it’s not possible to achieve this sort of grouping with notify-send, so if you’re hoping to get this working simply in a bash script you can just give up now I’m afraid.

The stacking/grouping of notifications is mentioned nowhere in the Desktop Notifications Spec - it is an implementation detail of each notification server. The way that applications like ABRT are getting around this is by first creating a GtkStatusIcon object to persist in the notification area. You can see this in the create_status_icon and new_warn_notification functions in the ABRT source.

So the only way to collect your notifications together is to write your own little program for each script which uses the GtkStatusIcon and NotifyNotification APIs via D-BUS. Obviously this isn’t ideal (it requires linking against libnotify for one thing, even for a tiny script).

However, it’s not that simple. GtkStatusIcon has been deprecated and the GIO GNotification API is being introduced. It looks like by using the GApplication class it’ll be possible to achieve a similar result to GtkStatusIcon with fewer dependencies and in a more future-proof way. Then again, maybe not. Until Gnome 3.14 (or possibly 3.16 with the updated notification area) sees wide release it will be difficult to tell.

So, in conclusion, grouping/stacking notifications à la ABRT or Transmission is a hack which depends upon the deprecated GtkStatusIcon. It can’t be done with notify-send and is thus impossible from simple scripts, but may become simpler in the future. Or it may not. I’ll update these notes if/when I eventually solve this.