How to use OS X Notification Center: Difference between revisions
(Created page with "== General Information == 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 w...") |
(Add to HowTo) |
||
Line 1: | Line 1: | ||
[[Category:HowTo]] | |||
== General Information == | == General Information == | ||
Revision as of 15:57, 25 November 2016
General Information
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.
- Create/rename file form *.cpp to *.mm
- Create method in *.h file of class which will post notification.
- 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.