KNotifications

KNotifications

KNotifications is a cross-platform library for creating popup notifications.

It currently supports Linux (and other Unix platforms that implement freedesktop.org notifications), Windows (8 or later), macOS and Android (version 5.0 or later).

Please consult the KDE Human Interface Guidelines for when using Notifications is appropriate.

KNotification is the main entry point for using KNotifications.

The global config file

In order to perform a notification, you need to create a description file, which contains default parameters of the notification. It needs to be installed to knotifications5/appname.notifyrc in a QStandardPaths::GenericDataLocation directory. On Android, this path is qrc:/knotifications5/.

The filename must either match QCoreApplication::applicationName or be specified as the component name to the KNotification object.

Warning
Notifications won't be visible otherwise.

You can do this with the following CMake command, if you use ECM's KDEInstallDirs:

install(FILES appname.notifyrc DESTINATION ${KDE_INSTALL_KNOTIFYRCDIR})

This file contains mainly 3 parts

  1. Global information
  2. Context information
  3. Definition of individual events

Global information

The global part looks like that

    [Global]
    IconName=myicon
    Name=Name of Application
    Comment=A brief description of the application
    DesktopEntry=DesktopFileName

The icon filename is just the name, without extension. It follows the same behavior as Icon= as defined in the Freedesktop Desktop Entry specification. The Name field may be used as the application name for popup, it may also be used to visualize the application's notification settings in KCModule instances. If Name is not present, Comment is used instead. Either must be present. Make sure to follow the HIG

The DesktopEntry field is required in order for the application to be listed in the notifications KCModule, and for its notifications to appear in the notification history. Ensure that its value matches the application's desktop file name, that the desktop file name is set in the QGuiApplication or KAboutData desktopFileName property, and that the desktop file is not marked as Hidden.

Context information

This part consists of hints for the configuration widget

    [Context/group]
    Name=Group name
    Comment=The name of the group for contacts
    [Context/folder]
    Name=Group name
 

The second part of the groupname is the context identifier. It should not contain special characters. The Name field is the one the user will see (and which is translated)

Definition of Events

The definition of the events forms the most important part of the config file

    [Event/newmail]
    Name=New E-Mail
    Comment=You have got a new email
    Contexts=folder,group
    Action=Sound|Popup
    [Event/contactOnline]
    Name=Contact Goes Online
    Comment=One of your contact has been connected
    Contexts=group
    Sound=filetoplay.ogg
    Action=None
    Urgency=Low
 

These are the default settings for each notifiable event. Action is the string representing the action. Actions can be added to KNotification as plugins, by deriving from KNotificationPlugin. At the time of writing, the following actions are available: Taskbar, Sound, Popup, Logfile, TTS, Execute. Actions can be combined by separating them with '|'.

Contexts is a comma separated list of possible context for this event.

Urgency can be any of: Low, Normal, Critical.

Example of code

This portion of code will fire the event for the "contactOnline" event

KNotification *notification = new KNotification("contactOnline");
notification->setText(i18n("The contact <i>%1</i> has gone online", contact->name());
notification->setPixmap(contact->pixmap());
notification->setActions({i18n("Open chat")});
const auto groups = contact->groups();
for (const QString &group : groups) {
notification->addContext("group", group);
}
connect(notification, QOverload<unsigned int>::of(&KNotification::activated), contact, &Contact::slotOpenChat);
notification->sendEvent();
void setPixmap(const QPixmap &pix)
Set the pixmap that will be shown in the popup.
void sendEvent()
Send the notification to the server.
QString i18n(const char *text, const TYPE &arg...)
void activated()
Emitted only when the default activation has occurred.
void setText(const QString &text)
Set the notification text that will appear in the popup.
void setActions(const QStringList &actions)
Set the list of actions shown in the popup.
void addContext(const Context &context)
append a context at the list of contexts, see KNotification::Context
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 22 2023 03:48:35 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.