Skip to content

Add sound property to APNSAlertNotificationContent #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/APNSCore/Alert/APNSAlertNotificationContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ 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
)

let encoder = JSONEncoder()
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
Expand Down