A micro class for the OS-X Notification Center in C++.

If you are writing a cross-platform application you are probably not going to use the Native OS-X app development tools, and you might end up with something in C++, for example with Qt. However, you will be unable to access some internal OS-X functionality. One way to deal with this is writing something in Apple’s Objective-C that talks to the internal OS-X SDK. In this case, here is a small proof-of-concept that makes it extremely painless just to push some std::string to the notification center.

Do notice that you should package your app first before seeing if it actually works. Just running the development binary might not work.

Anyway, below is a very minimal and suboptimal example, but you get the point.

! Find the most recent version on GitHub here:
https://github.com/TimZaman/toolkit/blob/master/notifier.h
https://github.com/TimZaman/toolkit/blob/master/notifier.mm

notifier.h

/**
--------------------------------------------------------------------------------
-   Module      :   notifier.h
-   Description :   A micro wrapper for the embedded OS-X notification center.
-   Author      :   Tim Zaman, 20-NOV-2015
--------------------------------------------------------------------------------
*/
 
/*
 
Copyright (c) 2015 Tim Zaman
 
Permission to use, copy, modify, distribute, and sell this software
for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation.
 
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 
*/
 
#ifndef NOTIFIER_H
#define NOTIFIER_H
 
class Notifier {
 public:
    void notify(std::string title, std::string message){
        notifyMM(title, message);
    }
 private:
#ifdef __APPLE__ 
        //Only apple can use the notification in the .mm class
    void notifyMM(std::string title, std::string message);
#else
    //On non-apple systems, use another notification system
    void notifyMM(std::string title, std::string message){
        // @TODO Display a notification on other platforms
    }
#endif 
 
};
 
#endif

keepalive.mm

/**
--------------------------------------------------------------------------------
-   Module      :   notifier.mm
-   Description :   A micro wrapper for the embedded OS-X notification center.
-   Author      :   Tim Zaman, 20-NOV-2015
--------------------------------------------------------------------------------
*/
 
#include "notifier.h"
#include <NSUserNotification.h>
 
void Notifier::notifyMM(std::string title, std::string message) {
	NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
	userNotification.title =  [NSString stringWithCString:title.c_str()  encoding:[NSString defaultCStringEncoding]];
	userNotification.informativeText =  [NSString stringWithCString:message.c_str()  encoding:[NSString defaultCStringEncoding]];
	NSUserNotificationCenter* center = [NSUserNotificationCenter defaultUserNotificationCenter];
	[center deliverNotification:userNotification];
}

In your CMakelists.txt just obviously add something along the lines of

add_executable( appname (...) <strong>notifier.mm</strong>)

Usage is obvious

Above code is free domain, or beerware, whatever.

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...