NSUserNotification没有显示操作按钮

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

我总是得到这个结果:

enter image description here

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


我无法在Patrick的答案下方添加评论,因为我只是stackoverflow中的新手。但是@Dorian,您可以在这里检查我的答案https://dev59.com/f3DYa4cB1Zd3GeqPGfxT#55320805 这个屏幕在系统偏好设置中。 - zhonglin
4个回答

50

正如之前的回答所述,为了显示操作按钮,通知类型需要设置为警告。如果您想将应用程序的默认通知样式设置为警报,则需要在info.plist中使用键NSUserNotificationAlertStyle定义值为alert

有关更多详细信息,请参见苹果公司的Info.plist键参考

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


1
你知道iOS上是否有类似的东西可用吗? - Patrick Perini
我不相信当前的API可以配置iOS中的UILocalNotifications。 - kurthardin.dev
5
应用程序需要签名才能允许按钮操作。 - RichS

27

这里是答案。

再次感谢freenode上的#macdev。

在此输入图像描述

要显示按钮,选择项需要为“Alerts”。


2
你是如何进入这个屏幕的? - Dorian
如何使用“_displayStyle”更改警告样式? - jigs

18

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

@property BOOL _showsButtons; // @dynamic _showsButtons;

所以你只需要将这行代码添加到你的程序中即可。

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

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


如何为通知中的按钮添加选择器? - Deepak Thakur
如何使用“_displayStyle”更改警报样式? - jigs

1

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

notification.set_showsButtons_(True)

cha-ching :)


在 PyObjC 中完美运行。 - kgutwin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接