我的猜测是nil
。确保您分配 (valid and non-nil)title
或informativeText
.
我想其他属性的值无效,例如otherButtonTitle
可能会阻止显示通知。
您的控制台中是否有任何错误消息?
使用调试器或NSLog()
语句来评估分配给通知的值。如果NSUserNotification
指针为 nil,您也可能会看到此问题(您发布的代码不是这种情况,但值得一提)。
这是适用于 Mac OS X 10.9 (Mavericks) 的应用程序委托的最小测试:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
unc.delegate = self;
NSUserNotification* notice = [[NSUserNotification alloc] init];
notice.title = @"title"; // notifications need a title, or informativeText at minimum to appear on screen
//notice.informativeText = @"informativeText";
NSLog(@"notification: %@, notification center:%@", notice, unc);
[unc deliverNotification:notice];
}
// The notifications will always dispaly even if we are in the foreground
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}