Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to use OS X Notification Center

From Qt Wiki
Jump to navigation Jump to search

General Information

Note: Since Qt 5.1 Notification Center messages can be posted using QSystemTrayIcon.

Starting from OS X 10.8 (Mountain Lion) all users how works on OS X were got access to receive notifications from applications and system. And how we can use this feature inside Qt application? This page will give to you instruction how to use OS X Notification center in your application.

Notification Center API is available from Objective-C/C++ code and you can use it from your Qt application if you are add Objective-C/C++ code to your application. Let's see how we can post message for class.

  1. Create/rename file form *.cpp to *.mm
  2. Create method in *.h file of class which will post notification.
  3. Create implementation for method. You can post notification with a few ways. You can send a simple text notification, text and description, with user's data and with delivery date.

Post simple Notification

The simple notification has a title and description and will looks like a text message. Code which post this notification looks like:

//......
#import <Foundation/Foundation.h>
//.....

void YourClassName::postNotifiacation(QString title, QString message) {
    NSUserNotification* notification = [[NSUserNotification alloc] init];
    notification.title = title.toNSString();
    notification.informativeText = message.toNSString();
    notification.soundName = NSUserNotificationDefaultSoundName;   //Will play a default sound
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
    [notification autorelease];
}

//With Subtitle
void YourClassName::postNotification(QString title, QString subtitle, QString message) {
    NSUserNotification* notification = [[NSUserNotification alloc] init];
    notification.title = title.toNSString();
    notification.subtitle = subtitle.toNSString();
    notification.informativeText = message.toNSString();
    notification.soundName = NSUserNotificationDefaultSoundName;   //Will play a default sound
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
    [notification autorelease];
}

Post notification with some data

You also can send notification with user's data which you can use in your app if user click to button. Code will looks like:

void YourClassName::postNotification(QString title, QString message, QVariant userData) {
    notification.title = title.toNSString();
    notification.informativeText = message.toNSString();
    notification.userInfo = //Need to covert QVaraint to NSDictionary;
    notification.soundName = NSUserNotificationDefaultSoundName;
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
    [notification autorelease];
}

Post notification with delivery date

Sample code looks like:

void YourClassName::postNotification(QString title, QString message, QDateTime deliveryDate) {
    NSUserNotification* notification = [[NSUserNotification alloc] init];
    notification.title = title.toNSString();
    notification.informativeText = message.toNSString();
    notification.deliveryDate = //Need to convert QDateTime to NSDate
    notification.soundName = NSUserNotificationDefaultSoundName;
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
    [notification autorelease];
}

That's it! Now you can use Notifications on your Qt application.