Namespaces
Variants
Actions

How to use standard Maemo 5 notifications in Qt applications

Jump to: navigation, search
Article Metadata

Article
Created: divanov (13 Jan 2010)
Last edited: hamishwillee (11 Oct 2012)

This article shows how to integrate with the Maemo 5 libnotify library, which provides a centralised and non-intrusive mechanism for supplying user notifications (desktop notifications are sent to a notification daemon, as defined in the Desktop Notifications spec).

The libnotify API can be found here: libnotify API or here: Maemo 5 libnotify API

libnotify is glib-based library, but you still can use it from Qt as it integrates glib event loop.

First step you need to add libnotify to your project. This is done by using pkg-config with qmake. Add these lines to your .pro file:

CONFIG += link_pkgconfig
PKGCONFIG += libnotify

The content of main.cpp file with minimal sample code:

#include <libnotify/notify.h>
 
#include <QApplication>
#include <QMainWindow>
#include <QDebug>
 
int
main (int argc,
char **argv)
{
const char name[] = "Qt sample notification app";
NotifyNotification *notification;
QApplication app(argc, argv);
QMainWindow win;
 
/* Init libnotify library */
notify_init(name);
 
win.show();
 
/* Create notification */
notification = notify_notification_new(name,
"Just want you to know...", NULL, NULL);
if (notification) {
/* Set timeout */
notify_notification_set_timeout(notification, 3000);
/* Schedule notification for showing */
if (!notify_notification_show(notification, NULL))
qDebug("Failed to send notification");
 
/* Clean up the memory */
g_object_unref(notification);
} else
qDebug("Failed to create notification");
 
return app.exec();
}

Screenshot:

Notification.png

Alternatively you can use libnotifymm C++ bindings to achieve the same goal:

CONFIG += link_pkgconfig
PKGCONFIG += libnotifymm-1.0 gtkmm-2.4

The content of main.cpp file with minimal sample code:

#include <libnotifymm/init.h>
#include <libnotifymm/notification.h>
 
#include <QApplication>
#include <QMainWindow>
#include <QDebug>
 
int
main (int argc,
char **argv)
{
const char name[] = "Qt sample notification app";
const char body[] = "Just want you to know...";
QApplication app(argc, argv);
QMainWindow win;
 
Notify::init(name);
 
Notify::Notification notification(name, body);
 
win.show();
 
notification.set_timeout(3000);
std::auto_ptr<Glib::Error> err(0);
if (!notification.show(err))
qDebug("Failed to send notification");
 
return app.exec();
}
This page was last modified on 11 October 2012, at 04:17.
153 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved