1

I have a helper application that is active when the GUI app is not. The helper application is posting notifications to [NSUserNotificationCenter defaultNotificationCenter]. I'd like to have the GUI app open when the user activates a notification from the helper. Is there a way to do this?

I'd also prefer that the helper app post notifications with the same name as the main app, but that's less of an issue than getting the didActivateNotification: sent to the right executable.

stevesliva
  • 5,351
  • 1
  • 16
  • 39

2 Answers2

1

You could simply open your main app via NSWorkspace in the didActivateNotification: delegate method:

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
    [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:@"/Applications/TextEdit.app"] options:0 configuration:nil error:nil];
}

This will open the app at the specified URL if it is not running, or bring it to the foreground if it is already open.

An alternative approach would be to register a custom URL scheme in your main app, and use NSWorkspace's openURL. This way you could define a simple action/parameter grammar that triggers different behaviour in your main app.

Community
  • 1
  • 1
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Good answer. First, duh, yeah, have the helper launch the main app, but then doh, you have to pass launch arguments or something to pass along the notification info. I think the URL scheme is the craftiest way of things if I'd stuck with having the helper post the notifications. What I ended up doing was delving more into figuring out how to have the GUI app enter a background mode when closed, and re-launch the GUI with notifications. – stevesliva Apr 04 '14 at 16:52
1

Posting notifications works only with running apps, that have registered for receiving these notifications.

If you want to launch your GUI app, you can use [ [ NSWorkspace sharedWorkSpace] launchApplication: pathToApplication]; pathToApplication should be set correct and complete, e.g. /applications/the GUIapp

The method must be implemented in your helper app, so reaction to the user activation has to be implemented there also.

I assume that you have set the Delegate for the NSUserNoticationCenter, so userNotificationCenter:didActivateNotification: is received. The discussion part of this link https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSUserNotificationCenterDelegate_Protocol/Reference/Reference.html#jumpTo_3 explains, how to receive and extract the information from an "userInfo" dict, which you can feed with the information, which app shall react, see this link for the userInfo https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSUserNotification_Class/Reference/Reference.html#jumpTo_17

I haven't used NSUserNotifications but maybe this helps also NSUserNotificationCenter dismiss notification.

Community
  • 1
  • 1
macrene
  • 198
  • 1
  • 8
  • Thanks. I was using the userInfo dict, which is why I hoped to deliver it to the GUI app from the helper. Alas. – stevesliva Apr 04 '14 at 16:54