0

I am trying to get a reply from a notification and pass the string to a label. But the label (nResponse) does not change after I give the reply, why isn't the label updating? Is there a problem with the "activationType" method?

AppDelegate.h

@property (weak) IBOutlet NSTextField *nTitle;
@property (weak) IBOutlet NSTextField *nMessage;
-(IBAction)showNotification:(id)sender;
@property (weak) IBOutlet NSTextField *nResponse;
@property (assign) IBOutlet NSWindow *window;
@property BOOL hasReplyButton NS_AVAILABLE(10_9, NA);
@property (copy) NSString *responsePlaceholder NS_AVAILABLE(10_9, NA);
@property (readonly) NSAttributedString *response NS_AVAILABLE(10_9, NA);
@property (copy) NSImage *contentImage NS_AVAILABLE(10_9, NA);
@end

AppDelegate.m

@synthesize nTitle;
@synthesize nMessage;
@synthesize nResponse;
@synthesize window;

- (IBAction)showNotification:(id)sender{
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = [nTitle stringValue];
    notification.informativeText = [nMessage stringValue];
    notification.soundName = NSUserNotificationDefaultSoundName;
    notification.responsePlaceholder = @"Reply";
    notification.hasReplyButton = true;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
{
    if (notification.activationType == NSUserNotificationActivationTypeReplied){
        NSString* userResponse = notification.response.string;
        [nResponse setStringValue:userResponse]; //set label to response
        NSLog(@"%@", userResponse);
    }
    return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

@end
kyle k
  • 5,134
  • 10
  • 31
  • 45
  • 1
    what does `NSLog` say about both `nResponse` and `userResponse` in your `userNotificationCenter:shouldPresentNotification:` method? – Brad Allred Nov 14 '13 at 01:54
  • @BradAllred Nothing is being showed to the debug console, I Updated my question. I think that the problem might be related to the "activationType". – kyle k Nov 14 '13 at 02:40
  • 1
    why are you doing the label update in that method instead of `userNotificationCenter:didActivateNotification:`? `NSUserNotificationActivationTypeReplied` should be fine as long as you are on 10.9. – Brad Allred Nov 14 '13 at 03:20
  • have you tried printing notification.response.string outside the if condition... i.e. try in anycase what does it prints – yunas Nov 16 '13 at 06:59
  • @yunas Yes i have, could not get it to work there ether. – kyle k Nov 16 '13 at 20:30
  • @kylek are you sure that the notification delegate method is being called on the main thread, where you should be updating your ui? dispatch_async(dispatch_get_main_queue(), ^{ [nResponse setStringValue:userResponse]; }); ` – Alex Shepard Nov 21 '13 at 03:36
  • @AlexShepard No i am not sure, but i will investigate if that is causing the problem, Thanks. – kyle k Nov 21 '13 at 03:38

1 Answers1

2
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;

You should also make sure your Notice class implements the NSUserNotificationCenterDelegate protocol.

codercat
  • 22,873
  • 9
  • 61
  • 85