문제

How do i get input from a notification with a reply field. I have not found anything in the documentation

Here is my current code, what do I need to get a response from the user?

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize nTitle;
@synthesize nMessage;

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

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
    return YES;
}

@end

Where can i find more recent information and documentation for the notification center?

도움이 되었습니까?

해결책

You can find the latest documentation in headers as usual. First of all, make sure you are using OSX SDK 10.9. There are few new fields with description.

NSUserNotification.h:

// Set to YES if the notification has a reply button. The default value is NO.
// If both this and hasActionButton are YES, the reply button will be shown.
@property BOOL hasReplyButton NS_AVAILABLE(10_9, NA);

// Optional placeholder for inline reply field.
@property (copy) NSString *responsePlaceholder NS_AVAILABLE(10_9, NA);

// When a notification has been responded to, the NSUserNotificationCenter delegate
// didActivateNotification: will be called with the notification with the activationType
// set to NSUserNotificationActivationTypeReplied and the response set on the response property
@property (readonly) NSAttributedString *response NS_AVAILABLE(10_9, NA);

Let's do it:

- (IBAction)showNotification:(id)sender{
NSUserNotification *notification = [[NSUserNotification alloc] init];
...
notification.responsePlaceholder = @"Reply";
notification.hasReplyButton = true;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
    if (notification.activationType == NSUserNotificationActivationTypeReplied){
        NSString* userResponse = notification.response.string;
    }
}

Notice that the reply button is hidden until mouse is outside a notification window and the reply field will be shown after button click.

다른 팁

You can find all the documentation you need in the inline documentation of Xcode 5 if you search "NSUserNotificationCenter", "NSUserNotification" and "NSUserNotificationCenterDelegate Protocol Reference".

There's no reply field in a user notification but you can add an action button and test if the user clicked this button or the default button to dismiss the notification. However, if in the Notification Center preferences, the user choose to receive banners instead of alerts, you'll never be informed of the response because there are no buttons in banners.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top