QT Notifications Chapter 2 – Using Snorenotify

Hope you managed to install KDE/snorenotify from my previous post. This time it’s talks to about a sample project to display notifications.

Actually I’ve just peaked on it’s test cpp file and reorganized it to send a test from UI. Snore example can get from my GitHub repository. For building project don’t forget to set LibsnoreSettingsQt5_DIR and LibsnoreQt5_DIR paths.

Set Libsnore paths

It’s just a simple QML screen has 2 text inputs and a button to trig a notification.

Let’s talk about SnoreCore. It’s used to manage and emit Notifications as Hannah said. Steps are clear get the SnoreCore instance, load plugins and set your values. whenever you register your application to SnoreCore you’re good to broadcast your notifications over it!

In this sample it works with Backend plugins for notification. Normally it returns a string list whenever plugin names called from your SnoreCore instance .pluginNames(SnorePlugin::Backend). Honestly I didn’t find this efficient because of it’s using all the backend plugin services that it can use. It’s not helpful for me, beside it’s a testing code. I’ve hardcoded available backends into a namespace.

namespace Backends{
    constexpr auto freeDesktop = "Freedesktop";
    constexpr auto snarl = "Snarl";
    constexpr auto snore = "Snore";
    constexpr auto trayIcon = "Trayicon";
    constexpr auto windowsToast = "Windows Toast";
}

used preprocessor to set primary notification backend for each OS:

    #ifdef Q_OS_WINDOWS
        instance.setPrimaryNotificationBackend(Backends::windowsToast);
    #elif defined(Q_OS_MACOS)
        instance.setPrimaryNotificationBackend(Backends::snarl);
    #else
        instance.setPrimaryNotificationBackend(Backends::freeDesktop);
    #endif

Created a Q_INVOKABLE method for QML calls:

void MyNotificationController::newNotification()
{
    auto &snore = SnoreCore::instance();
    snore.broadcastNotification(Notification(*app_, app_->defaultAlert(), title_, message_, app_->icon()));
}

Whenever “Send Notification” button is clicked it emits notification:

to observe the notifications that are invoked with user interaction there is 2 connections in MyNotificationController’s constructor:

    connect(&instance, &SnoreCore::actionInvoked, this, &MyNotificationController::handleActionInvoked);
    connect(&instance, &SnoreCore::notificationClosed, this, &MyNotificationController::handleNotificationClosed);

Point is to handle this signals that are emitted from SnoreCore to observe notification objects. Snore::Notification instances also have unique id as integers, which is a way to handle notifications one by one:

qDebug() << __LINE__ << __PRETTY_FUNCTION__ << notification.id();

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *