Skip to content

Commit 7cfeec8

Browse files
authored
Add ability to start live activities (#196)
* Add start case * Add ability to start live activities * Run swift-format * Remove unnecessary imports * Add alert to live activity start notifications * Remove additional conformances * Remove another conformance * formatting tweak * Move APNSLiveActivityNotificationEvent back to a struct. * format * Oops missed this one * format * remove unrelated change * remove unnecessary event from struct * Fix indentation * Remove unnecessary init
1 parent 3be05cd commit 7cfeec8

6 files changed

+251
-32
lines changed

Sources/APNSCore/LiveActivity/APNSLiveActivityNotification.swift

+7-8
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,34 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
3030
get {
3131
return self.aps.timestamp
3232
}
33-
33+
3434
set {
3535
self.aps.timestamp = newValue
3636
}
3737
}
38-
38+
3939
/// Event type e.g. update
4040
public var event: APNSLiveActivityNotificationEvent {
4141
get {
4242
return APNSLiveActivityNotificationEvent(rawValue: self.aps.event)
4343
}
44-
44+
4545
set {
4646
self.aps.event = newValue.rawValue
4747
}
4848
}
49-
49+
5050
/// The dynamic content of a Live Activity.
5151
public var contentState: ContentState {
5252
get {
5353
return self.aps.contentState
5454
}
55-
55+
5656
set {
5757
self.aps.contentState = newValue
5858
}
5959
}
60-
60+
6161
public var dismissalDate: APNSLiveActivityDismissalDate? {
6262
get {
6363
return .init(dismissal: self.aps.dismissalDate)
@@ -66,7 +66,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
6666
self.aps.dismissalDate = newValue?.dismissal
6767
}
6868
}
69-
69+
7070
/// A canonical UUID that identifies the notification. If there is an error sending the notification,
7171
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
7272
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
@@ -123,7 +123,6 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
123123
dismissalDate: dismissalDate
124124
)
125125
}
126-
127126

128127
/// Initializes a new ``APNSLiveActivityNotification``.
129128
///

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationAPSStorage.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable>: Encodabl
2424
var event: String
2525
var contentState: ContentState
2626
var dismissalDate: Int?
27-
27+
2828
init(
2929
timestamp: Int,
3030
event: String,
3131
contentState: ContentState,
3232
dismissalDate: Int?
3333
) {
3434
self.timestamp = timestamp
35-
self.event = event
3635
self.contentState = contentState
3736
self.dismissalDate = dismissalDate
37+
self.event = event
3838
}
3939
}

Sources/APNSCore/LiveActivity/APNSLiveActivityNotificationEvent.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
public struct APNSLiveActivityNotificationEvent: Hashable {
16-
1716
/// The underlying raw value that is send to APNs.
1817
@usableFromInline
1918
internal let rawValue: String
20-
19+
2120
/// Specifies that live activity should be updated
2221
public static let update = Self(rawValue: "update")
23-
22+
2423
/// Specifies that live activity should be ended
2524
public static let end = Self(rawValue: "end")
2625
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the APNSwift open source project
4+
//
5+
// Copyright (c) 2022 the APNSwift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of APNSwift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import struct Foundation.UUID
16+
17+
/// A notification that starts a live activity
18+
///
19+
/// It is **important** that you do not encode anything with the key `aps`.
20+
public struct APNSStartLiveActivityNotification<Attributes: Encodable, ContentState: Encodable>:
21+
APNSMessage
22+
{
23+
enum CodingKeys: CodingKey {
24+
case aps
25+
}
26+
27+
/// The fixed content to indicate that this is a background notification.
28+
private var aps: APNSStartLiveActivityNotificationAPSStorage<Attributes, ContentState>
29+
30+
/// Timestamp when sending notification
31+
public var timestamp: Int {
32+
get {
33+
return self.aps.timestamp
34+
}
35+
36+
set {
37+
self.aps.timestamp = newValue
38+
}
39+
}
40+
41+
public var alert: APNSAlertNotificationContent {
42+
get {
43+
return self.aps.alert
44+
}
45+
46+
set {
47+
self.aps.alert = newValue
48+
}
49+
}
50+
51+
/// The dynamic content of a Live Activity.
52+
public var contentState: ContentState {
53+
get {
54+
return self.aps.contentState
55+
}
56+
57+
set {
58+
self.aps.contentState = newValue
59+
}
60+
}
61+
62+
public var dismissalDate: APNSLiveActivityDismissalDate? {
63+
get {
64+
return .init(dismissal: self.aps.dismissalDate)
65+
}
66+
set {
67+
self.aps.dismissalDate = newValue?.dismissal
68+
}
69+
}
70+
71+
/// A canonical UUID that identifies the notification. If there is an error sending the notification,
72+
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
73+
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
74+
/// `123e4567-e89b-12d3-a456-42665544000`.
75+
///
76+
/// If you omit this, a new UUID is created by APNs and returned in the response.
77+
public var apnsID: UUID?
78+
79+
/// The date when the notification is no longer valid and can be discarded. If this value is not `none`,
80+
/// APNs stores the notification and tries to deliver it at least once,
81+
/// repeating the attempt as needed if it is unable to deliver the notification the first time.
82+
/// If the value is `immediately`, APNs treats the notification as if it expires immediately
83+
/// and does not store the notification or attempt to redeliver it.
84+
public var expiration: APNSNotificationExpiration
85+
86+
/// The priority of the notification.
87+
public var priority: APNSPriority
88+
89+
/// The topic for the notification. In general, the topic is your app’s bundle ID/app ID.
90+
public var topic: String
91+
92+
/// Initializes a new ``APNSStartLiveActivityNotification``.
93+
///
94+
/// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs.
95+
/// It is **important** that you do not encode anything with the key `aps`
96+
///
97+
/// - Parameters:
98+
/// - expiration: The date when the notification is no longer valid and can be discarded.
99+
/// - priority: The priority of the notification.
100+
/// - appID: Your app’s bundle ID/app ID. This will be suffixed with `.push-type.liveactivity`.
101+
/// - contentState: Updated content-state of live activity
102+
/// - timestamp: Timestamp when sending notification
103+
/// - dismissalDate: Timestamp when to dismiss live notification when sent with `end`, if in the past
104+
/// dismiss immediately
105+
/// - apnsID: A canonical UUID that identifies the notification.
106+
/// - attributes: The ActivityAttributes of the live activity to start
107+
/// - attributesType: The type name of the ActivityAttributes you want to send
108+
/// - alert: An alert that will be sent along with the notification
109+
public init(
110+
expiration: APNSNotificationExpiration,
111+
priority: APNSPriority,
112+
appID: String,
113+
contentState: ContentState,
114+
timestamp: Int,
115+
dismissalDate: APNSLiveActivityDismissalDate = .none,
116+
apnsID: UUID? = nil,
117+
attributes: Attributes,
118+
attributesType: String,
119+
alert: APNSAlertNotificationContent
120+
) {
121+
self.aps = APNSStartLiveActivityNotificationAPSStorage(
122+
timestamp: timestamp,
123+
contentState: contentState,
124+
dismissalDate: dismissalDate.dismissal,
125+
alert: alert,
126+
attributes: attributes,
127+
attributesType: attributesType
128+
)
129+
self.apnsID = apnsID
130+
self.expiration = expiration
131+
self.priority = priority
132+
self.topic = appID + ".push-type.liveactivity"
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the APNSwift open source project
4+
//
5+
// Copyright (c) 2022 the APNSwift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of APNSwift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
struct APNSStartLiveActivityNotificationAPSStorage<Attributes: Encodable, ContentState: Encodable>:
16+
Encodable
17+
{
18+
enum CodingKeys: String, CodingKey {
19+
case timestamp = "timestamp"
20+
case event = "event"
21+
case contentState = "content-state"
22+
case dismissalDate = "dismissal-date"
23+
case alert = "alert"
24+
case attributes = "attributes"
25+
case attributesType = "attributes-type"
26+
}
27+
28+
var timestamp: Int
29+
var event: String = "start"
30+
var contentState: ContentState
31+
var dismissalDate: Int?
32+
var alert: APNSAlertNotificationContent
33+
var attributes: Attributes
34+
var attributesType: String
35+
36+
init(
37+
timestamp: Int,
38+
contentState: ContentState,
39+
dismissalDate: Int?,
40+
alert: APNSAlertNotificationContent,
41+
attributes: Attributes,
42+
attributesType: String
43+
) {
44+
self.timestamp = timestamp
45+
self.contentState = contentState
46+
self.dismissalDate = dismissalDate
47+
self.alert = alert
48+
self.attributes = attributes
49+
self.attributesType = attributesType
50+
}
51+
}

0 commit comments

Comments
 (0)