Skip to content

timschroederme/ncMeta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ncMeta Project

ncMeta project is a set of classes for having fun with Mountain Lion’s Notification Center. In its present form, it offers the functionality

  • to monitor notifications being delivered to the Notification Center by any app;
  • to monitor Notification Center being invoked (and thereby brought to front) by the user.

I developed the ncMeta Project to facilitate interaction with Mountain Lion’s Notification Center beyond the limited functionality presently offered by the NSUserNotificationCenter class. The ncMeta Project doesn’t use any hacks or non-public methods.

How does it work?

The ncMeta Project consists of several classes, most of which are described below. However, you’ll typically only have to interact with the TSNCMetaController class and the TSNCMetaControllerDelegate Protocol.

Behind the scenes, the ncMeta Project reads from the sqlite database Notification Center uses to store information about notifications and the apps which deliver them. To monitor when Notification Center is in front, it sets a timer and regularly checks if its window (which doesn’t look like a window) is the front-most window.

The code contained in this repository includes a sample app that uses the ncMeta classes.

Usage

To use the ncMeta Project in your application, first

  • put all ncMeta files into your Xcode project,
  • put the fmdb classes mentioned below into your Xcode project,
  • if you want to sandbox your app, please read the sandbox caveats below,
  • link to the CoreServices framework and to the libsqlite3.dylib library,
  • read the General Caveats (see below).

Second, read and follow the documentation of the TSNCMetaController class and the TSNCMetaControllerDelegate Protocol. Off you go!

General Caveats

  • ncMeta Project will only compile with Xcode 4.4+ and needs ARC to be enabled.
  • ncMeta Project uses the fmdb framework and needs the following .h and .m files to be present in order to compile: FMDatabase, FMDatabaseAdditions, FMDatabasePool, FMDatabaseQueue, FMResultSet.

Sandbox Caveats

You can run the ncMeta Project classes in a sandbox. Add the temporary entitlement key com.apple.security.temporary-exception.files.home-relative-path.read-write with a value of /Library/Application Support/NotificationCenter/ to your entitlements plist file, or ask the user for access to that directory via an open dialog and store the security-scoped URL you receive for later access. Though ncMeta doesn’t write to Notification Center’s database, I’ve experienced issues with the fmdb framework corrupting Notification Center’s database when having only read-only access.

License

The license for ncMeta Project is contained in the license.md file.

Class Documentation

TSNCMetaController

TSNCMetaController is the only class you’ll have to interact with. As it provides interaction with an external ressource, the Notification Center, it has been designed as a singleton class.

Connect to the TSNCMetaController singleton using code like this:

TSNCMetaController *ncController = [TSNCMetaController sharedController];

Before using TSNCMetaController, you’ll have to set its delegate property, as most communication between the controller class and your application will be uni-directional, facilitated by the TSNCMetaControllerDelegate Protocol. So, to start monitor operations after having connected to the TSNCMetaController singleton, use this code:

[ncController setDelegate:self];
[ncController startOperations];

You’ll then receive information about Notification Center events via the TSNCMetaControllerDelegate Protocol.

-(void) startOperations

Begins monitoring Notification Center’s behaviour, i.e. if it is brought to front or if new notifications are delivered to it. If one of this happens, the delegate is called via the TSNCMetaControllerDelegate Protocol.

-(void) stopOperations

Stops monitoring Notification Center’s behaviour. This method should be called when the app terminates.

-(void)resetCount

If invoked, resets the internal count on how many notification have been delivered to the Notification Center by apps. This method is suitable to be called after the Notification Center has become visible.

-(void) setDelegate:(id)delegate

Sets TSNCMetaController’s delegate. The delegate must conform to the TSNCMetaControllerDelegate Protocol.

TSNCMetaControllerDelegate Protocol

The TSNCMetaControllerDelegate Protocol is used to inform the TSNCMetaController delegate about things happening to the Notification Center.

-(void) hasBecomeVisible

This delegate method is called when the Notification Center has become visible on the user’s screen.

-(void) hasBecomeInvisible

This delegate method is called when the Notification Center has been hidden by the user.

-(void) newNotifications:(NSArray*)newNotifications

This delegate method is called when new notifications have been delivered to and by the Notification Center. The array passed contains an object of the type TSAppRecord for every app which has delivered a notification.

TSAppRecord

TSAppRecord is the model class used to pass information on notifications delivered to the Notification Center by an app. It contains a number of public properties and one public method.

@property (strong) NSString *bundle_id

Contains the bundle id of the app which has delivered a notification to the Notification Center, e.g. com.apple.mail.

@property (assign) NSInteger show_count

Contains the maximum number of notifications that are shown in the Notification Center for the app, as set by the user in System Preferences.

@property (assign) BOOL shown

Is YES if the user has enabled Notification Center notifications for this app in System Preferences, NO if the user has disabled this behaviour.

@property (assign) NSInteger increase

Contains the number of notifications this app has delivered to the Notification Center since the last reset of this property. Calling TSNCMetaController -resetCount method resets this property for all TSAppRecord instances present. The value of this property may be greater than the value the show_count property returns.

-(NSString*) displayTitle

This convenience method returns the localized title of the application whose notification info is contained in a TSAppRecord instance.

About

Cocoa classes for having fun with Mountain Lion's Notification center, as well as a sample app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published