NSUserNotificationCenter

NSUserNotificationCenter

我正在尝试更新已传递的通知(警报)的副标题中的字符串,我正在使用NSTimer进行如下操作:

[NSTimer scheduledTimerWithTimeInterval:5.0
                                 target:self
                               selector:@selector(updateSubtitle)
                               userInfo:nil
                                repeats:YES];


- (void)updateSubtitle
{
    [[NSUserNotificationCenter defaultUserNotificationCenter].deliveredNotifications
        enumerateObjectsUsingBlock:^(NSUserNotification *notification, NSUInteger idx, BOOL *stop) {
            notification.subtitle = @"new subtitle";
    }];
}


该代码每5秒正确执行一次。但是通知警报中显示的字幕不会更改。

有什么方法可以强制像setNeedsDisplay这样的“重绘”或类似的东西吗?

谢谢。

最佳答案

我知道这篇文章有些旧,但是我最近试图弄清楚同一件事,但是最终需要删除现有的通知并添加一个具有相同标题的新通知。

在创建通知时,我在通知的标识符字段中使用NSUUID,然后使用类似(在Swift中)的方法找到现有的NSUUID:

var notif = NSUserNotification()
notif.identifier = <Id To Search For>
NSUserNotificationCenter.defaultUserNotificationCenter().removeScheduledNotification(notif)
NSUserNotificationCenter.defaultUserNotificationCenter().removeDeliveredNotification(notif)

10-08 05:35