我正在使用此代码:
- (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];
}
我得到了,但没有失败,
没有操作按钮或其他按钮。
如前面的答案中所述,需要将通知类型设置为警告要显示的操作按钮。如果要将应用程序的默认通知样式设置为警报,则需要在info.plist中使用值alert定义密钥NSUserNotificationAlertStyle。
有关详细信息,请参阅Apple的info.plist keys reference:
NSUserNotificationAlertStyle指定通知样式应为横幅,警报还是无。默认值为banners,这是推荐的样式。
这是答案。
再次感谢freenode上的#macdev。
选择需要是“警报”才能有按钮。
作为其他答案的相反例子,我们可以使用iTunes - 即使我们为横幅设置警报样式,它仍然显示“跳过”按钮。所以我继续搜索并找到this github repo,其中Indragie Karunaratne在NSUserNotification私有标头中提供了一些有用的附加属性。您可以在NSUserNotification_Private.h文件中检查属性的完整列表,但实际用于显示横幅通知样式中的按钮是
@property BOOL _showsButtons; // @dynamic _showsButtons;
所以你可以将这一行添加到你的代码中
[notification setValue:@YES forKey:@"_showsButtons"];
并且您的通知操作按钮将在警报样式上独立。
基于PARTISAN回复的魔术命令是:
notification.set_showsButtons_(True)
cha-ching :)