NSUserNotificationCenter ShouldPresentNotification - broken sample? RRS feed

  • Question

  • User473 posted

    I'm looking at the sample UserNotificationExample

    Notifications are fired and they end up in my notification center, however the notification doesn't get delivered when the app is the key window, despite the following:

    // If we return true here, Notification will show up even if your app is TopMost.
    center.ShouldPresentNotification = (c, n) => { return true; };

    I tried modifying the sample to manually supply the delegate for NSUserNotificationCenter and return true from there, but it also does nothing.

    The notification is delivered properly if the app is not the key window.

    Anyone has an idea? Thanks!

    Tuesday, March 26, 2013 7:37 PM

All replies

  • User4556 posted

    Hi - we have found exactly the same. Moreover this was behaving itself in Xam.Mac < 1.2 The behaviour seems different now - adding a log line in that delegate shows it is NEVER fired....

    Wednesday, March 27, 2013 4:56 PM
  • User1040 posted

    I just tried this sample, and I see notifications being delivered in the notification center and as popups when the window is key (ShouldPresentNotification is working as expected). Additionally, scheduling a notification directly on a background thread works.

    public override void AwakeFromNib ()
        base.AwakeFromNib ();
        var notifCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;
        notifCenter.ShouldPresentNotification = (c, n) => true;
        NotificationButton.Activated += (o, e) => {
            notifCenter.ScheduleNotification (new NSUserNotification {
                Title = "Test from main thread"
            NSThread.Start (() => notifCenter.ScheduleNotification (new NSUserNotification {
                Title = "Test from background thread"

    I'll look into our sample now.

    Thursday, March 28, 2013 2:57 PM
  • User1040 posted

    The UserNotificationExample project works as expected with no code changes. Are you sure you haven't changed your system notification preferences somehow?

    Thursday, March 28, 2013 3:02 PM
  • User473 posted

    Hi @abock

    Yes, my system notification preferences are in order. Like I say, the notifications work; the just don't present when the app is key, despite the ShouldPresentNotification callback returning true. Indeed, the callback is never actually called.

    I'm on OS X 10.8.3, with latest MonoMac built from source...

    Thursday, March 28, 2013 3:41 PM
  • User473 posted


    I just tested again. Your code sample dropped into a brand new app doesn't compile ('no overload for method 'start' takes '1' arguments'). However, commenting that out and just scheduling from the main thread shows the same behaviour. Notification appears in notification center, but I never get the popup.

    If I delay the call to ScheduleNotification by a few seconds, and switch to another app before if fires, I get the popup.

    Thursday, March 28, 2013 3:50 PM
  • User473 posted

    I re-downloaded the sample and ran it on my personal machine (a late 2008 unibody Macbook Pro w/10.8.3 and latest Xamarin), and it works fine.

    I tried again on my development machine (rMBP w/10.8.3 and latest Xamarin) and I don't get the notification bubble. It only appears in in the notification center.

    @IanPender what machine do you have?

    Friday, March 29, 2013 12:30 PM
  • User473 posted

    The equivalent objective C code works on both machines... Weird.

    Friday, March 29, 2013 1:47 PM
  • User4556 posted

    Wow nice work rjm, Yeah we're seeing some rather inconsistent and non-ideal behaviour - some people never see the first notification, - some never see the last, - some see all notifications

    We've a mix of OS. Most are MacMinis with 10.8.3 and Xamarin 10.2.

    Anyone from Xamarin see the same?

    Tuesday, April 2, 2013 3:29 PM
  • User4556 posted

    Well with nothing else to go on I'm trying to create and deliver the notification on a new thread... I suspect it's to do with UI thread coordination....

    Thursday, April 4, 2013 8:57 AM
  • User473 posted

    Whatever steps I take, including scheduling the notification on or off the UI thread; the delegate just doesn't work on one of my machines.

    The delegate methods are simply never invoked. I'm at a loss as to what can cause this.

    Thursday, April 4, 2013 1:30 PM
  • User4556 posted

    RJM - WE'VE SOLVED THIS. You'll need to keep a reference to the notification centre. If the notification centre is GCed before the notification is sent the delegate won't fire, so you'll get the default behaviour. Works everytime.

    Monday, April 8, 2013 7:16 AM
  • User473 posted

    @IanPender thanks for the update, glad you resolved the issue with notifications not getting delivered.

    Are the delegate methods called? If you use this:

    notifCenter.ShouldPresentNotification = (c, n) => true;

    Do you get the notification pop up, even when your app is topmost?

    Monday, April 8, 2013 1:40 PM
  • User4556 posted

    Hey dude, we've sorted it in the bugzilla entry - https://bugzilla.xamarin.com/show_bug.cgi?id=11456. In short we were overwriting the delegates in a clumsy and, frankly, stupid way. They behave just fine now. Hope it helps!

    Thursday, April 11, 2013 10:11 AM
  • User54336 posted


    Monday, July 14, 2014 8:07 AM