目前没有公开的API。因此,无法符合App Store的要求。
但是
作为我小型技术演示应用程序DiscoNotifier(我以对通知进行键盘LED闪烁响应)的一部分,我编写了一个DDUserNotificationCenterMonitor类
参见:https://github.com/Daij-Djan/DiscoNotifier/tree/master/DiscoNotifier
它使用FileSystemEvents和SQLite工作,并检查通知中心的数据库
它可以工作,并且数据库具有所有信息(表:presented_notifications),但是这很脆弱且私有
~/Library/Application Support/NotificationCenter/
。#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
NSString *pathToNCSupport = [@"~/Library/Application Support/NotificationCenter/" stringByExpandingTildeInPath];
NSError *error = nil;
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathToNCSupport error:&error]; //find the db
FMDatabase *database = nil;
for (NSString *child in contents) {
if([child.pathExtension isEqualToString:@"db"]) {
database = [FMDatabase databaseWithPath:[pathToNCSupport stringByAppendingPathComponent:child]];
if([database open]) {
printf("Opening Notification Center");
[database close];
break;
}
}
}
执行任何SQL查询:
if([database open]) {
FMResultSet *rs = [database executeQuery:@"select count(*) as cnt from presented_notifications"];
while ([rs next]) {
int cnt = [rs intForColumn:@"cnt"];
NSLog(@"Total Records :%d", cnt);
}
[database close];
}
在 Github 上完成项目。