Skip to content

Commit 93565bd

Browse files
committed
Add alert to live activity start notifications
1 parent e20b90a commit 93565bd

5 files changed

+40
-17
lines changed

Sources/APNSCore/Alert/APNSAlertNotificationContent.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// The information for displaying an alert.
16-
public struct APNSAlertNotificationContent: Encodable, Sendable {
16+
public struct APNSAlertNotificationContent: Encodable, Sendable, Hashable {
1717
public struct StringValue: Encodable, Hashable, Sendable {
1818
internal enum Configuration: Encodable, Hashable {
1919
case raw(String)

Sources/APNSCore/LiveActivity/APNSLiveActivityNotification.swift

+16-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Hashable &
3838
}
3939
}
4040

41+
public var alert: APNSAlertNotificationContent? {
42+
get {
43+
return self.aps.alert
44+
}
45+
46+
set {
47+
self.aps.alert = newValue
48+
}
49+
}
50+
4151
/// Event type e.g. update
4252
public var event: any APNSLiveActivityNotificationEvent {
4353
get {
@@ -48,14 +58,17 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Hashable &
4858
return APNSLiveActivityNotificationEventUpdate()
4959
default:
5060
guard let attributesType = self.aps.attributesType,
51-
let state = self.aps.attributesContent
61+
let state = self.aps.attributesContent,
62+
let alert = self.aps.alert
5263
else {
5364
// Default to update
5465
return APNSLiveActivityNotificationEventUpdate()
5566
}
5667

5768
return APNSLiveActivityNotificationEventStart(
58-
attributes: .init(type: attributesType, state: state))
69+
attributes: .init(type: attributesType, state: state),
70+
alert: alert
71+
)
5972
}
6073
}
6174

@@ -173,8 +186,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Hashable &
173186

174187
self.aps = APNSLiveActivityNotificationAPSStorage(
175188
timestamp: timestamp,
176-
event: event.rawValue,
177-
attributes: attributes,
189+
event: event,
178190
contentState: contentState,
179191
dismissalDate: dismissalDate.dismissal
180192
)

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationAPSStorage.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable & Hashable
2222
case dismissalDate = "dismissal-date"
2323
case attributesType = "attributes-type"
2424
case attributesContent = "attributes"
25+
case alert = "alert"
2526
}
2627

2728
var timestamp: Int
@@ -30,19 +31,23 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable & Hashable
3031
var attributesContent: ContentState?
3132
var contentState: ContentState
3233
var dismissalDate: Int?
34+
var alert: APNSAlertNotificationContent?
3335

3436
init(
3537
timestamp: Int,
36-
event: String,
37-
attributes: APNSLiveActivityNotificationEventStart<ContentState>.Attributes?,
38+
event: any APNSLiveActivityNotificationEvent,
3839
contentState: ContentState,
3940
dismissalDate: Int?
4041
) {
4142
self.timestamp = timestamp
42-
self.event = event
43-
self.attributesType = attributes?.type
44-
self.attributesContent = attributes?.state
4543
self.contentState = contentState
4644
self.dismissalDate = dismissalDate
45+
self.event = event.rawValue
46+
47+
if let event = event as? APNSLiveActivityNotificationEventStart<ContentState> {
48+
self.attributesType = event.attributes.type
49+
self.attributesContent = event.attributes.state
50+
self.alert = event.alert
51+
}
4752
}
4853
}

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationEvent.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public struct APNSLiveActivityNotificationEventStart<State: Encodable & Hashable
4444

4545
public let rawValue = "start"
4646
public let attributes: Attributes
47+
public let alert: APNSAlertNotificationContent
4748

48-
public init(attributes: Attributes) {
49+
public init(attributes: Attributes, alert: APNSAlertNotificationContent) {
4950
self.attributes = attributes
51+
self.alert = alert
5052
}
5153
}
5254

@@ -64,9 +66,11 @@ extension APNSLiveActivityNotificationEvent where Self == APNSLiveActivityNotifi
6466

6567
extension APNSLiveActivityNotificationEvent
6668
where Self: APNSLiveActivityNotificationEventStartStateProtocol {
67-
public static func start(type: String, state: State) -> APNSLiveActivityNotificationEventStart<
68-
State
69-
> {
70-
.init(attributes: .init(type: type, state: state))
69+
public static func start(type: String, state: State, alert: APNSAlertNotificationContent)
70+
-> APNSLiveActivityNotificationEventStart<
71+
State
72+
>
73+
{
74+
.init(attributes: .init(type: type, state: state), alert: alert)
7175
}
7276
}

Tests/APNSTests/LiveActivity/APNSLiveActivityNotificationTests.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ final class APNSLiveActivityNotificationTests: XCTestCase {
5353
contentState: State(),
5454
// Need the fully qualified name here
5555
event: APNSLiveActivityNotificationEventStart(
56-
attributes: .init(type: "State", state: State())),
56+
attributes: .init(type: "State", state: State()),
57+
alert: .init(title: .raw("Update"))
58+
),
5759
timestamp: 1_672_680_658)
5860

5961
let encoder = JSONEncoder()
6062
let data = try encoder.encode(notification)
6163

6264
let expectedJSONString = """
63-
{"aps":{"event":"start", "attributes-type": "State", "attributes": {"string":"Test","number":123},"content-state":{"string":"Test","number":123},"timestamp":1672680658}}
65+
{"aps":{"event":"start", "alert": { "title": "Update" }, "attributes-type": "State", "attributes": {"string":"Test","number":123},"content-state":{"string":"Test","number":123},"timestamp":1672680658}}
6466
"""
6567

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

0 commit comments

Comments
 (0)