Notification Content Extension and Swift Complete Concurrency

According to the documentation (https://developer.apple.com/documentation/usernotificationsui/unnotificationcontentextension), a Notification Content Extension should consist of a UIViewController that adopts the UNNotificationContentExtension protocol.

The only problem is that UNNotificationContentExtension's methods are not @MainActor isolated, but UIViewController is, which produces this error when you try to build your code with Complete concurrency checking turned on:

Main actor-isolated instance method 'didReceive' cannot be used to satisfy nonisolated protocol requirement

If you add nonisolated, you are then left with another problem in that UNNotification is not Sendable.

What is the recommended solution to this problem?

Answered by DTS Engineer in 797645022

There are two parts to this:

  • Are these protocol methods call on the main thread?

  • If so, how do you tell the compiler about that?

I’m not an expert on Notification Content extensions, so I asked some colleagues about the first point. They assure me that these methods are in fact called on the main thread.

Given that, this is another example of a common problem: A protocol that should be annotated with the @MainActor attribute but isn’t. I recently took the time to write up an explanation of this problem. You can find it in Implementing a Main Actor Protocol That’s Not @MainActor. Please read it through and let me know if you continue to have problems.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

There are two parts to this:

  • Are these protocol methods call on the main thread?

  • If so, how do you tell the compiler about that?

I’m not an expert on Notification Content extensions, so I asked some colleagues about the first point. They assure me that these methods are in fact called on the main thread.

Given that, this is another example of a common problem: A protocol that should be annotated with the @MainActor attribute but isn’t. I recently took the time to write up an explanation of this problem. You can find it in Implementing a Main Actor Protocol That’s Not @MainActor. Please read it through and let me know if you continue to have problems.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you, Quinn.

I did try nonisolated, but the problem is the part of the notification I need is [AnyHashable: Any] so that's where I got stuck.

I will go with preconcurrency.

Thanks again!

Notification Content Extension and Swift Complete Concurrency
 
 
Q