0

Something which seems very simple, but I struggle to trigger a simple user notification in MacOs.

What I want is a simple POC showing some "Hello world" at the NotificationCenter.

EDITED: With POC, I refer to an standalone software solving the main issues before the final implementation. It should not suggest short-cuts or unusable solutions, rather allow hard-coded content and simplified error management / configuration.

What did I tried already?

  • Created a CMakeList project
  • Included UserNotifications framework
  • Included UserNotifications/UserNotifications.h

The first issue I see is that content in UserNotifications.h is some objective-c or similar language (C++ does not have #import)

I cannot create any UNUserNotificatonCenter object or equivalent, they all are "undeclared".

I could not find any C++ example for this.

How to create a basic User Notification on MacOS?

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • You can do it from Terminal like this https://stackoverflow.com/a/30016842/2836621 or *"shell out"* from your C/C++ program with `system()` to call it. See `osascript` command specifically. – Mark Setchell Feb 12 '20 at 16:38
  • Are you serious? – Adrian Maire Feb 12 '20 at 16:39
  • There are a lot of questions and examples out there that explain how to combine C++ and Objective C in the same project. That seems to be the root of your issue, rather than with user notifications specifically... – TheNextman Feb 12 '20 at 16:42
  • Yes, it's a simple POC and can do what you ask. I'm not saying it's the best method, but it can keep you going till you find something better... it's not like you'll be sending thousands of notifications per second. – Mark Setchell Feb 12 '20 at 16:42
  • @TheNextman: No, I don't want to integrate Objective-c into my POC. I think the purpose of the "POC" is different for you an me, so I will edit the question. – Adrian Maire Feb 12 '20 at 16:47
  • The macOS API is Objective-c and/or Swift. That's it. You have to use an objective C call. (Of course there is also the POSIX API and some included cross-platform libraries like Bonjour which have a C API but that's not relevant here). – op414 Feb 12 '20 at 17:00
  • https://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-member-function – TheNextman Feb 12 '20 at 17:01
  • I am used to IOKit, CoreFunctations which have C++ equivalent (poorly documented, that is true) – Adrian Maire Feb 12 '20 at 17:02
  • If so, I guess the answer to my question is "You can't". Thank you all of you for your support. – Adrian Maire Feb 12 '20 at 17:07
  • Of course you can. But you either need to compile your code as Objective C++, or bridge the Objective C headers using C. The user space frameworks in Cocoa are Objective C and/or Swift, which you can't directly call from C++. That includes IOKit.framework. CoreFoundation is C. – TheNextman Feb 12 '20 at 17:55
  • Your question is really "how can I use Objective C objects and functions from C++?". Of which there are many existing questions and examples. – TheNextman Feb 12 '20 at 17:59

0 Answers0