Notifications in Swift

Oscar de la Hera Gomez
A flower that represents Swift next to a flower that represents XCode. Beneath it sits the text that states 'Notifications'.

A step by step tutorial on posting and receiving notifications in Swift (iOS). Github repository included.

The following tutorial builds on our Swift series that from scratch shows you how to create a SwiftLint enabled project, how to add Swift Package dependencies, create a swift based style manager (known as a styleguide) and localize your app for multiple languages.

If you are looking how to post and receive notifications, jump to Step three.

Step One: Create your Notifications file

A screenshot showing you how to create a file in Xcode for the SystemNotifications.swift file.

Under the Models folder, select the Notifications folder and press Command + N.

This will select the Swift, iOS file type. Press Next.

After that enter the name of your file - SystemNotifications.swift - and press Create.

Step Two: Code your struct and custom notification

A screenshot showing you the code for the SystemNotifications Struct and the custom notification.

In our case, we have called our struct SystemNotifications and the custom notification onContentUpdate.

A screenshot showing you how an Apple product interprets notifications and how it can be a source of error when debugging why notifications are not working as expected.

Please note that the string value on the right hand side is what is used by the system - so make sure you change both the constant name and the string value of the notification if copy-pasting, as this has been known as a source of bugs in the past.

Step Three: Post your notification

A screenshot showing you how to send a standard notification in swift.

If you are looking to send a standalone notification, without added information such as a variable or object, use the following code:

A screenshot showing you how to send a notification with a custom variable attached to it. The key is in the userInfo dictionary that is sent.

If you are looking to send a notification with added information, like a variable or object, place that variable or object in the userInfo in a dictionary form similar to the code below.

Step Four: Add the notification observer

A screenshot showing you how to add an observer to an object or view in Swift. In our case we added it to the CustomUIView we created in a past tutorial.

In the view, or object, where you wish to listen for the notification; add an observer similar to the one in the code below.

In our example, we have added it to the CustomView and pointed it to a function onSystemUpdatedContent that we define on Step 5.

Step Five: Process your notification

A screenshot showing you that we redirected our notification to content update. You can choose to respond to a notification as you like within the function that is set as its selector, on the observer added in the step below.

As shown in the image above, if you are looking to send a standalone notification, without added information such as a variable or object, add any code to the @objc selector that you have chosen. In our case, we redirect it to our onContentUpdate function.

A screenshot showing you how we used a guard to ensure that the userInfo exists and took the custom variable from the notification.

If you are looking to receive a notification with added information, like a variable or object use the code below. Please note that the key information between step four and five is the userInfo dictionary that is passed.

Step Six: Verify

A screenshot showing you the logs that were obtained when running the app. It shows that the custom variable was received through the notification.

Run your app and witness the notification being sent and received.

As you can see from our logs, the notification is both received includes the custom variable.

Any Questions

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details