Example

Here is a simple but complete example of the use of Hildon::Notification:

Figure 6.1. Notification

Notification

Source Code

File: main.cc

#include <hildonmm.h>
#include <hildon-notifymm.h>
#include <hildon/hildon-notification.h>
#include <libnotifymm/init.h>
#include <gtkmm/stock.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <iostream>

void on_button_show_notification()
{
  //Create a new notification:
  Glib::RefPtr<Hildon::Notification> notification = 
    Hildon::Notification::create("Something Happened", 
      "A thing has just happened that you might find interesting.", 
      Gtk::Stock::OPEN);

  // Show the notification:
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    notification->show();
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "Notification::show() failed: " << ex.what() << std::endl;
  }
  #else
  std::auto_ptr<Glib::Error> ex;
  notification->show(ex);
  if(ex.get())
  { 
    std::cerr << "Notification::show() failed: " << ex->what() << std::endl;
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

int main(int argc, char *argv[])
{
  // Initialize gtkmm and maemomm:
  Gtk::Main kit(&argc, &argv);
  Hildon::init();
  Hildon::notify_init("Notification example");

  // Initialize D-Bus (needed by hildon-notify):
  DBusConnection* conn = dbus_bus_get (DBUS_BUS_SESSION, NULL);
  dbus_connection_setup_with_g_main (conn, NULL);
  
  osso_context_t* osso_context = osso_initialize("example", "0.0.1", TRUE /* deprecated parameter */, 0 /* Use default Glib main loop context */);
  if(!osso_context)
    std::cerr << "osso_initialize() failed." << std::endl;

  Glib::set_application_name("Notification example");

  // Show a window with a button:
  Hildon::Window window;
  Hildon::Program::get_instance()->add_window(window);

  Gtk::Button button("Show Notification");
  window.add(button);
  button.show();

  // Show the notification when the button is clicked:
  button.signal_clicked().connect(sigc::ptr_fun(&on_button_show_notification));
  
  // Begin the application
  kit.run(window);

  osso_deinitialize(osso_context);
  
  return 0;
}