从OS X通知中心获取通知列表 [英] Getting notification list from OS X Notification Center

查看:198
本文介绍了从OS X通知中心获取通知列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在Mac上通知中心发送通知使用 NSUserNotification NSUserNotificationCenter API类。






但是是否有任何从通知中心阅读通知的方式?

解决方案

由于Daij-Djan的回答,我可以知道通知中心的首选项位于SQLite数据库〜/ Library / Application Support / NotificationCenter /



要读取此数据库,可以使用 FMDB ,您会在 pod a>。

  #importFMDatabase.h
#importFMDatabaseAdditions.h



获取数据库文件并打开它。

  NSString * pathToNCSupport = [@〜/ Library / Application Support / NotificationCenter /stringByExpandingTildeInPath]; 
NSError * error = nil;
NSArray * contents = [[NSFileManager defaultManager] contentOfDirectoryAtPath:pathToNCSupport错误:&错误]; //找到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查询: / p>

  if([database open]){
FMResultSet * rs = [database executeQuery:@select count as cnt from present_notifications];
while([rs next]){
int cnt = [rs intForColumn:@cnt];
NSLog(@总记录:%d,cnt);
}

[database close];
}

Github


It's possible to send notifications to the Notification Center on Mac using NSUserNotification and NSUserNotificationCenter API classes.


But is there any way of reading the notifications from the Notification Center?

解决方案

Thanks to Daij-Djan's answer, I could figure out that the preferences for the notification center are located in a SQLite database in ~/Library/Application Support/NotificationCenter/.

To read this database, we can use FMDB which you will find as a pod.

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"

Get the DB file and open it.

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;
        }
    }
}

Launch any SQL query:

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];
}

Complete project on Github.

这篇关于从OS X通知中心获取通知列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆