20

I'm trying to use the new Mountain Lion NSUserNotificationCenter for my application (which isn't too hard actually). Posting notifications works like a charm via

NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

However, i'd like to dismiss all notifications that are on the screen once the app gains focus. E.g. like the new Messages app does it. When new messages are received in the background, notifications are shown. When the app becomes active again, these are dismissed automatically and vanish from the screen and from the Notification Center.

To replicate this, I've registered a method to the NSApplicationDidBecomeActiveNotification notification which also gets called succesfully. In there I call [NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications].

This, however, has the effect that notifications that have been collected in the Notification Center are removed while the corresponding "bubbles" that are displayed in the top right corner are still displayed.

Iterating all delivered notifications and removing them each on their own has the exactly same effect, as has using scheduleNotification instead of deliverNotification.

Am I the only one experiencing this, or am I missing something to dismiss the on-screen part and the Notification Center part of a notification programatically?

BinaryBucks
  • 976
  • 7
  • 17
  • Sounds like you should rather report this as a bug on http://bugreport.apple.com. – Max Seelemann Aug 27 '12 at 18:26
  • Already did that. Not that anybody at Apple actually reads them i guess, but just for the sake of completion. Not sure if I'm not missing something here and it's a fault on my side though. – BinaryBucks Aug 28 '12 at 07:31
  • In what context are you using the notifications, do you need to have the 'bubbles' at all? – Oliver Cooper Aug 28 '12 at 07:37
  • Notifications are used in a chat app. When a message is received and the app is not active a notification (http://cl.ly/J28h) is displayed. When viewing a chat the notification in the notification center and its on-screen notification should be cleared. Now each message on-screen notification has to be clicked to make it go away which is quite anoying if you receive many messages. The messages app clears all notifications from a contact when clicking a single on-screen notification. Note: Notifications are removed form the Notification Center, just the on-screen ones stay. – BinaryBucks Aug 28 '12 at 08:03

3 Answers3

18

The Messages app is probably using the private NSUserNotificationCenter _removeAllDisplayedNotifications or _removeDisplayedNotification: method.

You can try to use these methods to test if this is what you are looking for. Just add this category interface to declare the methods:

@interface NSUserNotificationCenter (Private)
- (void)_removeAllDisplayedNotifications;
- (void)_removeDisplayedNotification:(NSUserNotification *)notification;
@end

Unfortunately, since these are undocumented methods, you can not use them in an app distributed through the App Store. If this is indeed what you are looking for, then you should file a bug and ask for these methods to become part of the public API.

0xced
  • 25,219
  • 10
  • 103
  • 255
  • Using these private methods does indeed work, thank you. I've no clue, why such methods are not part of the public API tbh. I'll file a bug report for it. – BinaryBucks Oct 04 '12 at 07:16
4

As of 10.9, the following methods remove any displayed notifications:

// Clear a delivered notification from the notification center. If the 
// notification is not in the delivered list, nothing happens.
- (void)removeDeliveredNotification:(NSUserNotification *)notification;

// Clear all delivered notifications for this application from the 
// notification center.
- (void)removeAllDeliveredNotifications;

The behavior seems to have changed since 10.8, as any displayed notifications are removed as well when these methods are called (thanks @0xced for clarification).

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • Are you sure they are the same? **Displayed** vs **Delivered**. On 10.8, their implementations are not the same. (I haven’t checked on 10.9) – 0xced Nov 27 '13 at 10:46
  • At least with a test I performed on 10.9, calling `-removeAllDeliveredNotifications` removed any notifications that were currently displayed. I guess that must not be the behavior on 10.9. Edited to reflect that, thanks. – sudo rm -rf Nov 27 '13 at 22:36
2

removeDeliveredNotification is removing the displayed notification for me (on 10.11), the caveat being the identifier on the notification must be set.

rjs
  • 41
  • 1
  • 1