NSUserNotification not showing action button
Asked Answered
M

4

40

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.

Megen answered 26/7, 2012 at 18:46 Comment(1)
I can not add comments just below Patrick's answer since I am just a new guy in stackoverflow, but @Dorian, you can check my answer in here #16030255 The screen is in the system preference.Camilia
M
27

And here was the answer.

Thanks again to #macdev on freenode.

enter image description here

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

Megen answered 26/7, 2012 at 19:29 Comment(2)
How did you get to this screen?Lecky
How to use "_displayStyle" to change Alert style?Birch
B
50

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.

Bloomsbury answered 17/8, 2012 at 20:45 Comment(3)
Do you know if anything like this is available for iOS?Megen
I don't believe this can be configured for UILocalNotifications in iOS with the current APIs.Bloomsbury
And, the app needs to be signed for it to allow buttons.Cerebral
M
27

And here was the answer.

Thanks again to #macdev on freenode.

enter image description here

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

Megen answered 26/7, 2012 at 19:29 Comment(2)
How did you get to this screen?Lecky
How to use "_displayStyle" to change Alert style?Birch
B
18

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.

Biotin answered 15/4, 2014 at 15:2 Comment(2)
how to add selector for the button in notification?Fallingout
How to use "_displayStyle" to change Alert style?Birch
P
1

magic command based on PARTISAN's reply is:

notification.set_showsButtons_(True)

cha-ching :)

Polychasium answered 22/10, 2015 at 8:26 Comment(1)
Worked perfectly in PyObjCAquaplane

© 2022 - 2024 — McMap. All rights reserved.