2012-07-26 52 views
39

我使用这个代码: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]; 
} 

而且我得到,没有失败,

enter image description here

无操作按钮或其他按钮。

回答

26

这里就是答案。再次

由于#macdev freenode上。

enter image description here

的选择需要 “警报” 有按钮。

+1

你是怎么到这个画面? – Dorian 2015-11-07 01:00:45

48

正如在前面的回答已经说过的,通知类型需要设置提醒的要显示的操作按钮。如果你想设置你的应用程序的默认的通知样式提醒,您需要与价值警报定义密钥NSUserNotificationAlertStyle在info.plist中。

见苹果的info.plist keys reference了解更多详情:

NSUserNotificationAlertStyle指定通知样式是否应该横幅警报,或没有。默认值是横幅,这是推荐的样式。

+1

你知道这样的事情是否适用于iOS吗? – 2012-08-21 18:22:20

+0

我不相信这可以在iOS中使用当前API配置UILocalNotifications。 – 2012-08-24 22:09:58

+5

而且,应用程序需要进行签名以允许按钮。 – RichS 2013-01-09 20:26:21

17

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

@property BOOL _showsButtons; // @dynamic _showsButtons; 

,所以你可以只添加这行到你的代码

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

和您的通知操作按钮将变为独立的警报风格。

+0

不幸的是,这在El Capitan中不起作用。 ( – 2015-10-26 03:53:35

+2

作品对我来说在埃尔卡皮坦.. – Dorian 2015-11-07 00:59:45

+0

如何为按钮通知加选择? – 2016-08-12 08:56:22

1

基于党派的回复魔法的命令是:

notification.set_showsButtons_(True) 

茶清:)

+0

在PyObjC完美工作 – kgutwin 2017-08-02 12:44:25