如何更新已发送的NSUserNotification中的字幕?

时间:2014-07-01 16:15:52

标签: macos cocoa nsusernotification 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或类似的东西“重绘”?

感谢。

1 个答案:

答案 0 :(得分:2)

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

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

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