NSUserNotification未显示操作按钮

Pat*_*ini 40 cocoa osx-mountain-lion nsusernotification

我正在使用此代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我得到了,但没有失败,

在此输入图像描述

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

小智 49

如前面的答案中所述,需要将通知类型设置为警告要显示的操作按钮.如果要将应用程序的默认通知样式设置为警报,则需要在info.plist中使用值alert定义密钥NSUserNotificationAlertStyle.

有关详细信息,请参阅Apple的info.plist密钥参考:

NSUserNotificationAlertStyle指定通知样式应为横幅,警报还是.默认值为banners,这是推荐的样式.

  • 而且,应用程序需要签名才能允许按钮. (5认同)

Pat*_*ini 26

这是答案.

再次感谢freenode上的#macdev.

在此输入图像描述

选择需要是"警报"才能有按钮.

  • 您是如何进入此屏幕的? (2认同)

小智 18

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

@property BOOL _showsButtons; // @dynamic _showsButtons;
Run Code Online (Sandbox Code Playgroud)

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

[notification setValue:@YES forKey:@"_showsButtons"];
Run Code Online (Sandbox Code Playgroud)

并且您的通知操作按钮将在警报样式上独立.