Show Notification in Foreground

I would like a notification to appear when the Xojo desktop app is in the foreground.

I have a messaging module that checks for messages periodically within a timer of the application. I would just like a notification to appear briefly to alert the user no matter what window is open.

I am using macoslib and can get the notification to appear in the background, but can’t figure out how to get it to appear in the foreground.

I have the following code:

[code] //use macoslib to show a notification on Mac os x
dim m as new NSUserNotification
m.Title = "Message from: " + rs.Field(“msg_from”).StringValue
m.Subtitle = rs.Field(“subject”).StringValue
m.DeliveryDate = new Date

      dim n as new NSUserNotificationCenter
      n.DeliverNotification m[/code]

I don’t see an option for NSUserNotificationCenter for the message to appear in the foreground.

NSUserNotificationCenter says
For example, it may suppress the notification if the application is already frontmost (the delegate can override this action). The application can check the result of this decision by examining the isPresented property of a delivered user notification.

So in order to present it you would have to override the delegate method

User Notification Display Override
func userNotificationCenter(NSUserNotificationCenter, shouldPresent: NSUserNotification)
Sent to the delegate when the user notification center has decided not to present your notification.

What Norman says.

Not exactly sure how to set this setting in code. Can you give me an example?

I watched Kem Tekinay’s video on macoslib and did see the event related to this called “ShouldPresentNotificationInForeground”. He created an object: NCenter in a window where he placed the code in the event ShouldPresentNotificationInForeground as Return True.

Since I am not working within a window, I instead created a class: NCenter with the super: Cocoa.NSUserNotificationCenter. I then added an Event Handler: ShouldPresentNotificationInForeground in which I placed the code Return True.

Unfortunately, this did not work for me. Did I do something incorrectly?

Yes - I’ll post an example of how to do this tomorrow.

Take a look at this project:

Please note that I have a very old version of MacOSLib so you’ll want to remove the folder there and add your own version to make sure it works. When you press the button there is a bit of a delay so that you can put the app in the background or leave it in the foreground before the notification is delivered. If you want to see more examples of delegates, check out my iOS library, iOSKit:

[quote=336887:@Jason King]Take a look at this project: [/quote]
Thanks, Jason. It did work.

Looks like the coding is a little beyond my scope. Is there a some reference or documentation to understand this further?

Unfortunately not really. It’s mostly stuff I’ve figured out from looking through MacOSLib and way too much Apple documentation. I’ve been meaning to write something up about it for a while. Maybe one of these days…