+44 (791) 5698 086

Show notification in cocoa xcode app using Swift 3.0.1

This tutorial explains how to show a notification on macOS app.

This code will work for a cocoa app built using xcode using swift 3.0.1.

This code is written and verified on macbook pro running:

XCode 8.2

Swift 3.0.1

MacOS Sierra 10.12.2

How to show notification in cocoa xcode app using Swift 3.0.1

Follow these steps to show notifications in your cocoa app built using xcode using swift language code.

1. Conform to NSUserNotificationCenterDelegate

First step is to make your current class conform to NSUserNotificationCenterDelegate. For example if you’re putting the notification code in AppDelegate class in AppDelegate.swift file, then you will do it like this:

class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

2. Add custom method to show notifications

Add this method (functions inside a class are called method)

add this method:

Example 1:

    func showNotification() -> Void {

        var notification = NSUserNotification()

        // All these values are optional
        notification.title = "Test of notification"
        notification.subtitle = "Subtitle of notifications"
        notification.informativeText = "Main informative text"
        notification.contentImage = contentImage
        notification.soundName = NSUserNotificationDefaultSoundName

        NSUserNotificationCenter.default.deliver(notification)

    }

This will show something like this:

Show notification in cocoa xcode app using Swift 3.0.1

This will also appear in notifications centre:

Example 2:

If you want to dynamically change the values and send them to the method, you can do that too. The method then becomes:

    func showNotification(title: String, subtitle: String, informativeText: String, contentImage: NSImage) -> Void {

        var notification = NSUserNotification()

        notification.title = title
        notification.subtitle = subtitle
        notification.informativeText = informativeText
        notification.contentImage = contentImage
        notification.soundName = NSUserNotificationDefaultSoundName

        NSUserNotificationCenter.default.deliver(notification)

    }

Explanation of the code

What this code does is, that our method is defined with a return type of void, which means there won’t be any return from this method. We can skip this step too, however just a good coding practice imho.

Then we define our notifications variable equal to NSUserNotification object

Then we make changes to the properties of this object where needed. All fields are optional and we need to add and change the ones that we want only.

You can find the complete list of properties supported by NSUserNotification at: Apple documentation for NSUserNotification.

I hope this tutorial helps you generate dynamic notifications in your cocoa app for MacOS coded in swift 3.0.1 using xcode. Let me know if you have any suggestions, questions or corrections to thisNSUserNotification example code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Who are we?

Nabtron team includes web, desktop software and mobile apps developers along with SEO professionals.

We are available 24 hours, 7 days a week. Contact us now for anything you need!

Join our mailing list

Lets become friend :) What's your name & email?