Skip to content

Commit 1618cd9

Browse files
authored
Add sound property to APNSAlertNotificationContent (#226)
1 parent 0f10b6c commit 1618cd9

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Sources/APNSCore/Alert/APNSAlertNotificationContent.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public struct APNSAlertNotificationContent: Encodable, Sendable {
6060
case subtitleLocalizationArguments = "subtitle-loc-args"
6161
case bodyLocalizationKey = "loc-key"
6262
case bodyLocalizationArguments = "loc-args"
63+
case sound
6364
}
6465

6566
/// 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 {
7677
/// the contents of the specified image or storyboard file are displayed instead of your app’s normal launch image.
7778
public var launchImage: String?
7879

80+
/// For regular notifications, use ``APNSAlertNotificationSound/fileName(_:)`` to specify the name of a sound file in your app's main bundle
81+
/// or in the Library/Sounds folder of your app's container directory.
82+
/// Use ``APNSAlertNotificationSound/default`` to play the system sound.
83+
/// Use this key for regular notifications. For critical alerts, use ``APNSAlertNotificationSound/critical(fileName:volume:)`` instead.
84+
public var sound: APNSAlertNotificationSound?
85+
7986
/// Initializes a new ``APNSAlertNotificationContent``.
8087
///
8188
/// - Parameters:
@@ -87,12 +94,14 @@ public struct APNSAlertNotificationContent: Encodable, Sendable {
8794
title: APNSAlertNotificationContent.StringValue? = nil,
8895
subtitle: APNSAlertNotificationContent.StringValue? = nil,
8996
body: APNSAlertNotificationContent.StringValue? = nil,
90-
launchImage: String? = nil
97+
launchImage: String? = nil,
98+
sound: APNSAlertNotificationSound? = nil
9199
) {
92100
self.title = title
93101
self.subtitle = subtitle
94102
self.body = body
95103
self.launchImage = launchImage
104+
self.sound = sound
96105
}
97106

98107
public func encode(to encoder: Encoder) throws {
@@ -120,6 +129,7 @@ public struct APNSAlertNotificationContent: Encodable, Sendable {
120129
localizedArgumentsKey: .bodyLocalizationArguments
121130
)
122131
try container.encodeIfPresent(self.launchImage, forKey: .launchImage)
132+
try container.encodeIfPresent(self.sound, forKey: .sound)
123133
}
124134

125135
private func encode(

Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ final class APNSLiveActivityNotificationTests: XCTestCase {
8181
appID: "test.app.id",
8282
contentState: State(),
8383
event: .update,
84-
alert: .init(title: .raw("Hi"), body: .raw("Hello")),
84+
alert: .init(title: .raw("Hi"), body: .raw("Hello"), sound: .default),
8585
timestamp: 1_672_680_658
8686
)
8787

8888
let encoder = JSONEncoder()
8989
let data = try encoder.encode(notification)
9090

9191
let expectedJSONString = """
92-
{"aps":{"event":"update", "alert": { "title": "Hi", "body": "Hello" },"content-state":{"string":"Test","number":123},"timestamp":1672680658}}
92+
{"aps":{"event":"update", "alert": { "title": "Hi", "body": "Hello", "sound": "default" },\
93+
"content-state":{"string":"Test","number":123},"timestamp":1672680658}}
9394
"""
9495

9596
let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary

0 commit comments

Comments
 (0)