<!--
{
  "documentType" : "article",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/notification-center-messages",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Notification center messages"
}
-->

# Notification center messages

Use Foundation’s notification center with Swift concurrency.

## Discussion

In Swift, the [`Notification`](/documentation/Foundation/Notification) type is `nonisolated`, even in cases where it’s posted on a known isolation. To provide specific isolation information and better support Swift concurrency, `NotificationCenter` defines two message types. A [`NotificationCenter.MainActorMessage`](/documentation/Foundation/NotificationCenter/MainActorMessage) binds to the main actor, whereas a [`NotificationCenter.AsyncMessage`](/documentation/Foundation/NotificationCenter/AsyncMessage) uses an arbitrary isolation. Frameworks extend these types to define distinct messages, typically corresponding to an existing [`Notification.Name`](/documentation/Foundation/Notification/Name-swift.typealias), that declare instance properties for their values instead of using a `userInfo` dictionary. As a result, messages can conform to <doc://com.apple.documentation/documentation/Swift/Sendable> when they either don’t use properties or contain only sendable properties.

If your project only needs to support Swift, you can just use the `Message` types. For projects with both Objective-C and Swift code, define a [`Notification`](/documentation/Foundation/Notification) as well as a corresponding `Message` type.

### Posting and observing messages

You can post a message with the `post(_:subject:)` method, passing a message instance and optionally providing a subject. To receive messages, add an observer with the `addObserver(of:for:using:)` method. The overloads of this method allow you to observe either messages from a single object or from any object of a given type. Observation ends when you discard the token returned from `addObserver(of:for:using:)` or after an explicit call to [`removeObserver(_:)`](/documentation/Foundation/NotificationCenter/removeObserver(_:)-2gmm0).

You can also receive messages as an <doc://com.apple.documentation/documentation/Swift/AsyncSequence> with the `messages(of:for:bufferSize:)` methods.

Several flavors of `addObserver(of:for:using:)` and `messages(of:for:bufferSize:)` take a [`NotificationCenter.MessageIdentifier`](/documentation/Foundation/NotificationCenter/MessageIdentifier), which frameworks may implement to provide a typed, ergonomic experience at the call point for convenience, as described in [SE-0299](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md).

### Using messages with the SDK

Many system frameworks — including Foundation, UIKit, and AppKit — adopt the [`NotificationCenter.MainActorMessage`](/documentation/Foundation/NotificationCenter/MainActorMessage) and [`NotificationCenter.AsyncMessage`](/documentation/Foundation/NotificationCenter/AsyncMessage) types to facilitate using their notifications in concurrency-safe Swift code. The following example shows how to observe Foundation’s [`TimeZone`](/documentation/Foundation/TimeZone) type for the [`systemTimeZoneDidChange`](/documentation/Foundation/NotificationCenter/MessageIdentifier/systemTimeZoneDidChange) message, which contains the property [`previousTimeZone`](/documentation/Foundation/TimeZone/SystemTimeZoneDidChangeMessage/previousTimeZone).

```
/// Note: Store `token` in a property for as long as you intend to observe.
token = NotificationCenter.default.addObserver(of: TimeZone.self,
                                               for: .systemTimeZoneDidChange)
{ message in
    let identifier = message.previousTimeZone?.identifier ?? "(unknown)"
    print("Time zone changed from \(identifier).")
}
```

For use with existing notification code, [`NotificationCenter.MainActorMessage`](/documentation/Foundation/NotificationCenter/MainActorMessage) and [`NotificationCenter.AsyncMessage`](/documentation/Foundation/NotificationCenter/AsyncMessage) define helper methods that conforming types implement to convert a message to a corresponding notification and vice versa.

## Topics

### Declaring a message

[`NotificationCenter.MainActorMessage`](/documentation/Foundation/NotificationCenter/MainActorMessage)

A protocol for creating types that you can post to a notification center and bind to the main actor.

[`NotificationCenter.AsyncMessage`](/documentation/Foundation/NotificationCenter/AsyncMessage)

A protocol for creating types that you can post to a notification center, which posts them to an arbitrary isolation.

### Using message identifiers

[`NotificationCenter.MessageIdentifier`](/documentation/Foundation/NotificationCenter/MessageIdentifier)

An optional identifier to associate a given message with a given type.

[`NotificationCenter.BaseMessageIdentifier`](/documentation/Foundation/NotificationCenter/BaseMessageIdentifier)

A type for use when defining optional Message identifiers.

### Observing concurrency-safe notifications

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-4d19x)

Adds an observer to a center for messages delivered on the main actor with a given subject and identifier.   

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-90os)

Adds an observer to a center for messages delivered on the main actor with a given subject and identifier.   

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-56bn4)

Adds an observer to a center for messages delivered on the main actor with a given subject and message type.   

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-twm3)

Adds an observer to a center for messages delivered asynchronously with a given subject and identifier.   

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-t1wr)

Adds an observer to a center for messages delivered asynchronously with a given subject and message type.   

[`addObserver(of:for:using:)`](/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-64uw3)

Adds an observer to a center for messages delivered asynchronously with a given subject and message type.   

[`removeObserver(_:)`](/documentation/Foundation/NotificationCenter/removeObserver(_:)-2gmm0)

Stops the observation represented by the given observation token.

[`NotificationCenter.ObservationToken`](/documentation/Foundation/NotificationCenter/ObservationToken)

A unique token representing a single observer registration in a notification center.

### Receiving notifications as asynchronous sequences

[`messages(of:for:bufferSize:)`](/documentation/Foundation/NotificationCenter/messages(of:for:bufferSize:)-4tof0)

Returns an asynchronous sequence of messages produced by this center for a given subject and identifier.   

[`messages(of:for:bufferSize:)`](/documentation/Foundation/NotificationCenter/messages(of:for:bufferSize:)-1ub69)

Returns an asynchronous sequence of messages produced by this center for a given subject type and identifier.   

[`messages(of:for:bufferSize:)`](/documentation/Foundation/NotificationCenter/messages(of:for:bufferSize:)-623kg)

Returns an asynchronous sequence of messages produced by this center for a given subject and message type.   

### Posting notification messages

[`post(_:subject:)`](/documentation/Foundation/NotificationCenter/post(_:subject:)-87dbk)

Posts a given main actor message to the notification center.   

[`post(_:)`](/documentation/Foundation/NotificationCenter/post(_:)-19s7b)

Posts a given main actor message to the notification center.   

[`post(_:subject:)`](/documentation/Foundation/NotificationCenter/post(_:subject:)-5271w)

Posts a given asynchronous message to the notification center.   

[`post(_:)`](/documentation/Foundation/NotificationCenter/post(_:)-7ia4j)

Posts a given asynchronous message to the notification center.   



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)