【问题标题】:Getting notification list from OS X Notification Center从 OS X 通知中心获取通知列表
【发布时间】:2014-10-11 00:23:45
【问题描述】:

可以使用NSUserNotificationNSUserNotificationCenter API 类将send notifications 发送到Mac 上的通知中心。


但是有任何方法可以从通知中心读取通知吗?

【问题讨论】:

  • 为了澄清,阅读您拥有/计划的用户通知非常简单。但是,其他通知是个问题。
  • 你说得对。我没有提到我需要收到 any 通知。

标签: objective-c macos cocoa osx-mavericks


【解决方案1】:

感谢 Daij-Djan 的回答,我知道通知中心的首选项位于~/Library/Application Support/NotificationCenter/ 的 SQLite 数据库中。

要读取这个数据库,我们可以使用FMDB,你会发现它是pod

#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 上完成项目。

【讨论】:

  • 这种方法在 2020 年仍然有效吗?如果没有,现在有办法吗?
【解决方案2】:

没有公开的 API。所以没有任何 App Store 符合。

但是

作为我的小型技术演示应用 DiscoNotifier 的一部分(我会闪烁键盘 LED 以响应通知),我编写了一个 DDUserNotificationCenterMonitor 类

见:https://github.com/Daij-Djan/DiscoNotifier/tree/master/DiscoNotifier

它使用 FileSystemEvents 和 SQLite 并检查通知中心的数据库

它可以工作,并且数据库包含所有信息(表:presented_notifications)但是..这是脆弱且私密的

【讨论】:

    猜你喜欢
    • 2015-12-27
    • 2014-01-06
    • 2014-01-18
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多