0

Is there a way to clear all Notifications that has notificationName = AVSystemController_SystemVolumeDidChangeNotification from the NSNotificationCenter.defaultCenter()?

I looked into many questions and I found a way to clear local notifications (UILocalNotification not NSNotifications) using `cancelLocalNotification(notification). I also found a way to clear ALL notifications from the notification center. I also looked into notification center documentation but didn't find something about that.

Note: Here is why I want it: I want to add an observer for volume change (when the user presses on the volume buttons) and I use (AVSystemController_SystemVolumeDidChangeNotification).

However, when I add it, I have to wait half a second (dispatch_after) to avoid any accumulating button presses from the user that happened right before (or while) adding the observer. In other words, the observer finds "old" notifications that were in the notification center before I tell it to start "observing".

So, I don't want to block main thread. Instead, I want to clear all the volume change notifications before I add the observer (this way I can be sure that no 'old' notification will trigger my actions).

I want to clear a specific type of notifications that has this notificationName.

Any ideas how to do it in Swift?

CSawy
  • 904
  • 2
  • 14
  • 25
  • You are confusing UILocalNotification and NSNotification. `cancelLocalNotification` removes UILocalNotifications from the notification screen on the device. NSNotifications before your observer is added are not delivered to your observer. Any volume change notifications you receive occurred after your observer was added. – Paulw11 Nov 09 '15 at 19:52
  • 1
    You might investigate whether observer the system volume with KVO gives you better behavior. http://stackoverflow.com/questions/9660445/detect-iphone-volume-button-up-press/22601280#22601280 – Paul Cantrell Nov 09 '15 at 19:53
  • Thank you @Paulw11. I understand the difference. I am just saying all the questions I found were related to UILocalNotifications not NSNotifications. And yes, the old notifications should not be added but sometimes they are. I tested it many times. – CSawy Nov 09 '15 at 20:11
  • How big is the gap between establishing your AVAudioSession and registering the volume notification observer? There may be some lag in delivering volume changes from the system down to your app but there is nothing you can do about this; there is no way of clearing the queue. Perhaps you could register an observer that does nothing with the event and then remove this and add the "real" observer after a second or two – Paulw11 Nov 09 '15 at 20:16

0 Answers0