- 1 year ago
How to display an icon in a Notification Center message?
Comment
1 / 1
To display an icon in a Notification Center message on macOS, you can use the NSUserNotification
class in Swift or Objective-C. Here's an example:
Swift:
swiftlet notification = NSUserNotification()
notification.title = "Title"
notification.subtitle = "Subtitle"
notification.informativeText = "Message"
notification.soundName = NSUserNotificationDefaultSoundName
notification.contentImage = NSImage(named: "icon.png") // Set the image here
NSUserNotificationCenter.default.deliver(notification)
Objective-C:
objcNSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"Title";
notification.subtitle = @"Subtitle";
notification.informativeText = @"Message";
notification.soundName = NSUserNotificationDefaultSoundName;
notification.contentImage = [NSImage imageNamed:@"icon.png"]; // Set the image here
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
In both examples, the contentImage
property of the NSUserNotification
class is set to the image you want to display as the icon in the notification. You can use any image file in your project as the icon. Once you have set the properties of the notification, you can deliver it to the Notification Center using the deliver
method of the NSUserNotificationCenter
class.