40

我正在使用这段代码:

- (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];
}

我得到,没有失败,

在此处输入图像描述

没有操作按钮或其他按钮。

4

4 回答 4

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.

于 2012-08-17T20:45:43.303 回答
27

这就是答案。

再次感谢 freenode 上的#macdev。

在此处输入图像描述

选择需要是“警报”才能有按钮。

于 2012-07-26T19:29:28.330 回答
18

作为其他答案的相反实例,我们可以使用 iTunes - 即使我们为横幅设置警报样式,它仍然显示“跳过”按钮。所以我继续搜索并找到了这个 github repo,其中Indragie Karunaratne在 NSUserNotification 私有标头中提供了一些有用的附加属性。您可以检查NSUserNotification_Private.h文件中的完整属性列表,但实际以横幅通知样式显示按钮的是

@property BOOL _showsButtons; // @dynamic _showsButtons;

所以你可以将这一行添加到你的代码中

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

并且您的通知操作按钮将独立于警报样式。

于 2014-04-15T15:02:39.870 回答
1

基于 PARTISAN 回复的魔术命令是:

notification.set_showsButtons_(True)

查清 :)

于 2015-10-22T08:26:04.363 回答