【问题标题】:NSUserNotification not showing action buttonNSUserNotification 未显示操作按钮
【发布时间】:2012-07-25 10:28:15
【问题描述】:

我正在使用此代码:

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

我得到了,没有失败,

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

【问题讨论】:

  • 我无法在 Patrick 的答案下方添加 cmets,因为我只是 stackoverflow 中的一个新人,但是@Dorian,您可以在这里查看我的答案stackoverflow.com/questions/16029755/… 屏幕在系统偏好设置中。

标签: cocoa osx-mountain-lion nsusernotification


【解决方案1】:

如上一个答案所述,需要将通知类型设置为警报以显示操作按钮。如果您想将应用的默认通知样式设置为警报,则需要在 info.plist 中定义键 NSUserNotificationAlertStyle,其值为 alert

详情请参阅 Apple 的info.plist keys reference

NSUserNotificationAlertStyle 指定通知样式是bannersalerts还是none。默认值为banners,这是推荐的样式。

【讨论】:

  • 你知道这样的东西是否适用于 iOS 吗?
  • 我不相信使用当前 API 可以为 iOS 中的 UILocalNotifications 进行配置。
  • 而且,应用需要签名才能允许按钮。
【解决方案2】:

这就是答案。

再次感谢 freenode 上的#macdev。

选择必须是“警报”才能有按钮。

【讨论】:

  • 你是怎么进入这个屏幕的?
  • 如何使用“_displayStyle”改变Alert样式?
【解决方案3】:

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

@property BOOL _showsButtons; // @dynamic _showsButtons;

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

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

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

【讨论】:

  • 如何在通知中为按钮添加选择器?
  • 如何使用“_displayStyle”改变Alert样式?
【解决方案4】:

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

notification.set_showsButtons_(True)

恰清:)

【讨论】:

  • 在 PyObjC 中完美运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 2022-06-25
  • 2020-12-18
  • 1970-01-01
相关资源
最近更新 更多