我不确定我是否弄错了,但从示例中我可以找到。我写了那个小helloworld。
#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>
int main (int argc, const char * argv[])
{
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
return 0;
}我用以下命令编译它:
cc -framework Foundation -o app main.m它编译了,我可以执行它,但它不会显示任何东西。由于某些原因,没有提供任何内容。我读到,如果应用程序是主要参与者,通知可能不会显示。如何使其显示通知?
发布于 2013-05-06 20:07:00
设置委托和重写
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}发布于 2018-05-11 10:14:05
有关Objective-C和Swift中的完整示例:
我们需要提供一个NSUserNotificationCenterDelegate,在最简单的情况下,我们可以使用AppDelegate实现这一点
Objective-C
我们需要修改提供NSUserNotificationCenterDelegate方法的AppDelegate。从干净的AppDelegate (看起来像这样...)
@interface AppDelegate ()我们将其修改为:
@interface AppDelegate () <NSUserNotificationCenterDelegate>现在,我们可以在@implementation AppDelegate中提供所需的委托方法
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}Swift 4
您可以使用AppDelegate的extension来提供NSUserNotificationCenterDelegate方法:
extension AppDelegate: NSUserNotificationCenterDelegate {
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}
}发布于 2018-11-24 08:23:17
请确保为任何新通知分配新的标识符。一个通知只会显示一次,除非使用了新的标识符或用户清除了其通知历史记录。
https://stackoverflow.com/questions/15896348
复制相似问题