Skip to content

Commit 6f26339

Browse files
committed
Add content-available to alert
1 parent 9b5032e commit 6f26339

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

Sources/APNSCore/Alert/APNSAlertNotification.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
130130
}
131131
}
132132

133+
/// The content available flag, when set to `1`, the notification will wake up your app in the background and trigger `didReceiveRemoteNotification`.
134+
///
135+
/// The highest score gets featured in the notification summary
136+
public var contentAvailable: Int? {
137+
get {
138+
self.aps.contentAvailable
139+
}
140+
set {
141+
self.aps.contentAvailable = newValue
142+
}
143+
}
144+
133145
/// A canonical UUID that identifies the notification. If there is an error sending the notification,
134146
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
135147
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
@@ -176,6 +188,7 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
176188
/// - targetContentID: The identifier of the window brought forward.
177189
/// - interruptionLevel: A string that indicates the importance and delivery timing of a notification.
178190
/// - relevanceScore: The relevance score, a number between `0` and `1`, that the system uses to sort the notifications from your app.
191+
/// - contentAvailable: The content available flag, when set to `1`, the notification will wake up your app in the background and trigger `didReceiveRemoteNotification`.
179192
/// - apnsID: A canonical UUID that identifies the notification.
180193
public init(
181194
alert: APNSAlertNotificationContent,
@@ -191,6 +204,7 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
191204
targetContentID: String? = nil,
192205
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
193206
relevanceScore: Double? = nil,
207+
contentAvailable: Int? = nil,
194208
apnsID: UUID? = nil
195209
) {
196210
self.aps = APNSAlertNotificationAPSStorage(
@@ -202,7 +216,8 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
202216
mutableContent: mutableContent,
203217
targetContentID: targetContentID,
204218
interruptionLevel: interruptionLevel,
205-
relevanceScore: relevanceScore
219+
relevanceScore: relevanceScore,
220+
contentAvailable: contentAvailable
206221
)
207222
self.apnsID = apnsID
208223
self.expiration = expiration
@@ -239,6 +254,7 @@ extension APNSAlertNotification where Payload == EmptyPayload {
239254
/// - targetContentID: The identifier of the window brought forward.
240255
/// - interruptionLevel: A string that indicates the importance and delivery timing of a notification.
241256
/// - relevanceScore: The relevance score, a number between `0` and `1`, that the system uses to sort the notifications from your app.
257+
/// - contentAvailable: The content available flag, when set to `1`, the notification will wake up your app in the background and trigger `didReceiveRemoteNotification`.
242258
/// - apnsID: A canonical UUID that identifies the notification.
243259
public init(
244260
alert: APNSAlertNotificationContent,
@@ -253,6 +269,7 @@ extension APNSAlertNotification where Payload == EmptyPayload {
253269
targetContentID: String? = nil,
254270
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
255271
relevanceScore: Double? = nil,
272+
contentAvailable: Int? = nil,
256273
apnsID: UUID? = nil
257274
) {
258275
self.aps = APNSAlertNotificationAPSStorage(
@@ -264,7 +281,8 @@ extension APNSAlertNotification where Payload == EmptyPayload {
264281
mutableContent: mutableContent,
265282
targetContentID: targetContentID,
266283
interruptionLevel: interruptionLevel,
267-
relevanceScore: relevanceScore
284+
relevanceScore: relevanceScore,
285+
contentAvailable: contentAvailable
268286
)
269287
self.apnsID = apnsID
270288
self.expiration = expiration

Sources/APNSCore/Alert/APNSAlertNotificationAPSStorage.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
2323
case targetContentID = "target-content-id"
2424
case interruptionLevel = "interruption-level"
2525
case relevanceScore = "relevance-score"
26+
case contentAvailable = "content-available"
2627
}
2728

2829
var alert: APNSAlertNotificationContent
@@ -49,6 +50,8 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
4950
}
5051
}
5152

53+
var contentAvailable: Int?
54+
5255
init(
5356
alert: APNSAlertNotificationContent,
5457
badge: Int? = nil,
@@ -58,7 +61,8 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
5861
mutableContent: Double? = nil,
5962
targetContentID: String? = nil,
6063
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
61-
relevanceScore: Double? = nil
64+
relevanceScore: Double? = nil,
65+
contentAvailable: Int? = nil
6266
) {
6367
if let relevanceScore = relevanceScore {
6468
precondition(relevanceScore >= 0 && relevanceScore <= 1, "The relevance score can only be between 0 and 1")
@@ -72,5 +76,6 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
7276
self.targetContentID = targetContentID
7377
self.interruptionLevel = interruptionLevel
7478
self.relevanceScore = relevanceScore
79+
self.contentAvailable = contentAvailable
7580
}
7681
}

Tests/APNSTests/Alert/APNSAlertNotificationTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ final class APNSAlertNotificationTests: XCTestCase {
8686
targetContentID: "targetContentID",
8787
interruptionLevel: .critical,
8888
relevanceScore: 1,
89+
contentAvailable: 1,
8990
apnsID: .init()
9091
)
9192
let encoder = JSONEncoder()
9293
let data = try encoder.encode(notification)
9394

9495
let expectedJSONString = """
95-
{\"payload\":\"payload\",\"aps\":{\"category\":\"category\",\"relevance-score\":1,\"badge\":1,\"target-content-id\":\"targetContentID\",\"sound\":\"default\",\"interruption-level\":\"critical\",\"alert\":{\"body\":\"body\",\"subtitle-loc-key\":\"subtitle-key\",\"title\":\"title\",\"launch-image\":\"launchimage\",\"subtitle-loc-args\":[\"arg1\"]},\"thread-id\":\"threadID\",\"mutable-content\":1}}
96+
{\"payload\":\"payload\",\"aps\":{\"category\":\"category\",\"relevance-score\":1,\"content-available\":1,\"badge\":1,\"target-content-id\":\"targetContentID\",\"sound\":\"default\",\"interruption-level\":\"critical\",\"alert\":{\"body\":\"body\",\"subtitle-loc-key\":\"subtitle-key\",\"title\":\"title\",\"launch-image\":\"launchimage\",\"subtitle-loc-args\":[\"arg1\"]},\"thread-id\":\"threadID\",\"mutable-content\":1}}
9697
"""
9798
let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary
9899
let jsonObject2 = try JSONSerialization.jsonObject(with: expectedJSONString.data(using: .utf8)!) as! NSDictionary
@@ -125,13 +126,14 @@ final class APNSAlertNotificationTests: XCTestCase {
125126
targetContentID: "targetContentID",
126127
interruptionLevel: .critical,
127128
relevanceScore: 1,
129+
contentAvailable: 1,
128130
apnsID: .init()
129131
)
130132
let encoder = JSONEncoder()
131133
let data = try encoder.encode(notification)
132134

133135
let expectedJSONString = """
134-
{\"payload\":\"payload\",\"aps\":{\"category\":\"category\",\"relevance-score\":1,\"badge\":1,\"target-content-id\":\"targetContentID\",\"sound\":{\"name\":\"file\",\"volume\":1,\"critical\":1},\"interruption-level\":\"critical\",\"alert\":{\"body\":\"body\",\"subtitle-loc-key\":\"subtitle-key\",\"title\":\"title\",\"launch-image\":\"launchimage\",\"subtitle-loc-args\":[\"arg1\"]},\"thread-id\":\"threadID\",\"mutable-content\":1}}
136+
{\"payload\":\"payload\",\"aps\":{\"category\":\"category\",\"relevance-score\":1,\"content-available\":1,\"badge\":1,\"target-content-id\":\"targetContentID\",\"sound\":{\"name\":\"file\",\"volume\":1,\"critical\":1},\"interruption-level\":\"critical\",\"alert\":{\"body\":\"body\",\"subtitle-loc-key\":\"subtitle-key\",\"title\":\"title\",\"launch-image\":\"launchimage\",\"subtitle-loc-args\":[\"arg1\"]},\"thread-id\":\"threadID\",\"mutable-content\":1}}
135137
"""
136138
let jsonObject1 = try JSONSerialization.jsonObject(with: data) as! NSDictionary
137139
let jsonObject2 = try JSONSerialization.jsonObject(with: expectedJSONString.data(using: .utf8)!) as! NSDictionary

0 commit comments

Comments
 (0)