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.

hire best wordpress php css developer
Need Help? Hire me now!
I can help you in anything related to webdevelopment, including: HTML, CSS, PHP & MySql, WordPress (plugins & themes development, others), regular expressions, server management, etc.

2 comments on “Show notification in cocoa xcode app using Swift 3.0.1

  1. This leaves out the necessary steps of implementing this function to make sure the notification actually displays. Otherwise it only shows up in the notification center.

    func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
        return true
    }
    

    And then when your controller loads:

    NSUserNotificationCenter.default.delegate = self
    

    And P.S. – Your adblock blocker only pushes people away from your site or makes them disable Javascript :)

Leave a Reply

Your email address will not be published.