Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserNotification not showing action button

I'm using this code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    [notification setTitle: @"Title"];
    [notification setSubtitle: @"Subtitle"];
    [notification setInformativeText: @"Informative Text"];

    [notification setHasActionButton: YES];
    [notification setActionButtonTitle: @"Action Button"];
    [notification setOtherButtonTitle: @"Other Button"];

    [notification setSoundName: NSUserNotificationDefaultSoundName];

    [notification setDeliveryDate: [NSDate dateWithTimeIntervalSinceNow: 10]];
    [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification: notification];
}

And I'm getting, without fail,

enter image description here

No action button, or other button.

like image 857
Patrick Perini Avatar asked Jul 26 '12 18:07

Patrick Perini


4 Answers

As already stated in a previous answer, the notification type needs to be set to alert for the action button to be shown. If you want to set the default notification style of your app to alert, you need to define the key NSUserNotificationAlertStyle in info.plist with the value alert.

See Apple's info.plist keys reference for more details:

NSUserNotificationAlertStyle Specifies whether the notification style should be banners, alerts, or none. The default value is banners, which is the recommended style.

like image 90
kurthardin.dev Avatar answered Oct 22 '22 19:10

kurthardin.dev


And here was the answer.

Thanks again to #macdev on freenode.

enter image description here

The selection needs to be "Alerts" to have buttons.

like image 39
Patrick Perini Avatar answered Oct 22 '22 21:10

Patrick Perini


As a contrary instance for other answers we can use iTunes - it still showing "Skip" button even when we setup alert style to banners. So I continued searching and found this github repo where Indragie Karunaratne provide some useful additional properties in NSUserNotification private headers. You can check for full list of the properties in the NSUserNotification_Private.h file, but actual for showing buttons in banner notification style is

@property BOOL _showsButtons; // @dynamic _showsButtons;

so you can just add this line to you code

[notification setValue:@YES forKey:@"_showsButtons"];

and your notification action button will become independent on alert style.

like image 18
PARTISAN Avatar answered Oct 22 '22 19:10

PARTISAN


magic command based on PARTISAN's reply is:

notification.set_showsButtons_(True)

cha-ching :)

like image 1
amohr Avatar answered Oct 22 '22 19:10

amohr