From b1f804a4d28d1ae35aa904c55241b317e1e27ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kre=C3=9F?= Date: Mon, 4 Aug 2025 17:01:23 +0200 Subject: [PATCH] Adds sound property to APNSAlertNotificationContent --- .../Alert/APNSAlertNotificationContent.swift | 12 +++++++++++- .../APNSLiveActivityNotificationTests.swift | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Sources/APNSCore/Alert/APNSAlertNotificationContent.swift b/Sources/APNSCore/Alert/APNSAlertNotificationContent.swift index ab0f81e1..c8064c92 100644 --- a/Sources/APNSCore/Alert/APNSAlertNotificationContent.swift +++ b/Sources/APNSCore/Alert/APNSAlertNotificationContent.swift @@ -60,6 +60,7 @@ public struct APNSAlertNotificationContent: Encodable, Sendable { case subtitleLocalizationArguments = "subtitle-loc-args" case bodyLocalizationKey = "loc-key" case bodyLocalizationArguments = "loc-args" + case sound } /// The title of the notification. Apple Watch displays this string in the short look notification interface. @@ -76,6 +77,12 @@ public struct APNSAlertNotificationContent: Encodable, Sendable { /// the contents of the specified image or storyboard file are displayed instead of your app’s normal launch image. public var launchImage: String? + /// For regular notifications, use ``APNSAlertNotificationSound/fileName(_:)`` to specify the name of a sound file in your app's main bundle + /// or in the Library/Sounds folder of your app's container directory. + /// Use ``APNSAlertNotificationSound/default`` to play the system sound. + /// Use this key for regular notifications. For critical alerts, use ``APNSAlertNotificationSound/critical(fileName:volume:)`` instead. + public var sound: APNSAlertNotificationSound? + /// Initializes a new ``APNSAlertNotificationContent``. /// /// - Parameters: @@ -87,12 +94,14 @@ public struct APNSAlertNotificationContent: Encodable, Sendable { title: APNSAlertNotificationContent.StringValue? = nil, subtitle: APNSAlertNotificationContent.StringValue? = nil, body: APNSAlertNotificationContent.StringValue? = nil, - launchImage: String? = nil + launchImage: String? = nil, + sound: APNSAlertNotificationSound? = nil, ) { self.title = title self.subtitle = subtitle self.body = body self.launchImage = launchImage + self.sound = sound } public func encode(to encoder: Encoder) throws { @@ -120,6 +129,7 @@ public struct APNSAlertNotificationContent: Encodable, Sendable { localizedArgumentsKey: .bodyLocalizationArguments ) try container.encodeIfPresent(self.launchImage, forKey: .launchImage) + try container.encodeIfPresent(self.sound, forKey: .sound) } private func encode( diff --git a/Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift b/Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift index 8f52ff84..a9da0ed4 100644 --- a/Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift +++ b/Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift @@ -81,7 +81,7 @@ final class APNSLiveActivityNotificationTests: XCTestCase { appID: "test.app.id", contentState: State(), event: .update, - alert: .init(title: .raw("Hi"), body: .raw("Hello")), + alert: .init(title: .raw("Hi"), body: .raw("Hello"), sound: .default), timestamp: 1_672_680_658 ) @@ -89,7 +89,8 @@ final class APNSLiveActivityNotificationTests: XCTestCase { let data = try encoder.encode(notification) let expectedJSONString = """ - {"aps":{"event":"update", "alert": { "title": "Hi", "body": "Hello" },"content-state":{"string":"Test","number":123},"timestamp":1672680658}} + {"aps":{"event":"update", "alert": { "title": "Hi", "body": "Hello", "sound": "default" },\ + "content-state":{"string":"Test","number":123},"timestamp":1672680658}} """ let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary