-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
251 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
Sources/APNSCore/LiveActivity/APNSStartLiveActivityNotification.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the APNSwift open source project | ||
// | ||
// Copyright (c) 2022 the APNSwift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of APNSwift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import struct Foundation.UUID | ||
|
||
/// A notification that starts a live activity | ||
/// | ||
/// It is **important** that you do not encode anything with the key `aps`. | ||
public struct APNSStartLiveActivityNotification<Attributes: Encodable, ContentState: Encodable>: | ||
APNSMessage | ||
{ | ||
enum CodingKeys: CodingKey { | ||
case aps | ||
} | ||
|
||
/// The fixed content to indicate that this is a background notification. | ||
private var aps: APNSStartLiveActivityNotificationAPSStorage<Attributes, ContentState> | ||
|
||
/// Timestamp when sending notification | ||
public var timestamp: Int { | ||
get { | ||
return self.aps.timestamp | ||
} | ||
|
||
set { | ||
self.aps.timestamp = newValue | ||
} | ||
} | ||
|
||
public var alert: APNSAlertNotificationContent { | ||
get { | ||
return self.aps.alert | ||
} | ||
|
||
set { | ||
self.aps.alert = newValue | ||
} | ||
} | ||
|
||
/// The dynamic content of a Live Activity. | ||
public var contentState: ContentState { | ||
get { | ||
return self.aps.contentState | ||
} | ||
|
||
set { | ||
self.aps.contentState = newValue | ||
} | ||
} | ||
|
||
public var dismissalDate: APNSLiveActivityDismissalDate? { | ||
get { | ||
return .init(dismissal: self.aps.dismissalDate) | ||
} | ||
set { | ||
self.aps.dismissalDate = newValue?.dismissal | ||
} | ||
} | ||
|
||
/// A canonical UUID that identifies the notification. If there is an error sending the notification, | ||
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits, | ||
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows: | ||
/// `123e4567-e89b-12d3-a456-42665544000`. | ||
/// | ||
/// If you omit this, a new UUID is created by APNs and returned in the response. | ||
public var apnsID: UUID? | ||
|
||
/// The date when the notification is no longer valid and can be discarded. If this value is not `none`, | ||
/// APNs stores the notification and tries to deliver it at least once, | ||
/// repeating the attempt as needed if it is unable to deliver the notification the first time. | ||
/// If the value is `immediately`, APNs treats the notification as if it expires immediately | ||
/// and does not store the notification or attempt to redeliver it. | ||
public var expiration: APNSNotificationExpiration | ||
|
||
/// The priority of the notification. | ||
public var priority: APNSPriority | ||
|
||
/// The topic for the notification. In general, the topic is your app’s bundle ID/app ID. | ||
public var topic: String | ||
|
||
/// Initializes a new ``APNSStartLiveActivityNotification``. | ||
/// | ||
/// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs. | ||
/// It is **important** that you do not encode anything with the key `aps` | ||
/// | ||
/// - Parameters: | ||
/// - expiration: The date when the notification is no longer valid and can be discarded. | ||
/// - priority: The priority of the notification. | ||
/// - appID: Your app’s bundle ID/app ID. This will be suffixed with `.push-type.liveactivity`. | ||
/// - contentState: Updated content-state of live activity | ||
/// - timestamp: Timestamp when sending notification | ||
/// - dismissalDate: Timestamp when to dismiss live notification when sent with `end`, if in the past | ||
/// dismiss immediately | ||
/// - apnsID: A canonical UUID that identifies the notification. | ||
/// - attributes: The ActivityAttributes of the live activity to start | ||
/// - attributesType: The type name of the ActivityAttributes you want to send | ||
/// - alert: An alert that will be sent along with the notification | ||
public init( | ||
expiration: APNSNotificationExpiration, | ||
priority: APNSPriority, | ||
appID: String, | ||
contentState: ContentState, | ||
timestamp: Int, | ||
dismissalDate: APNSLiveActivityDismissalDate = .none, | ||
apnsID: UUID? = nil, | ||
attributes: Attributes, | ||
attributesType: String, | ||
alert: APNSAlertNotificationContent | ||
) { | ||
self.aps = APNSStartLiveActivityNotificationAPSStorage( | ||
timestamp: timestamp, | ||
contentState: contentState, | ||
dismissalDate: dismissalDate.dismissal, | ||
alert: alert, | ||
attributes: attributes, | ||
attributesType: attributesType | ||
) | ||
self.apnsID = apnsID | ||
self.expiration = expiration | ||
self.priority = priority | ||
self.topic = appID + ".push-type.liveactivity" | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Sources/APNSCore/LiveActivity/APNSStartLiveActivityNotificationAPSStorage.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the APNSwift open source project | ||
// | ||
// Copyright (c) 2022 the APNSwift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of APNSwift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct APNSStartLiveActivityNotificationAPSStorage<Attributes: Encodable, ContentState: Encodable>: | ||
Encodable | ||
{ | ||
enum CodingKeys: String, CodingKey { | ||
case timestamp = "timestamp" | ||
case event = "event" | ||
case contentState = "content-state" | ||
case dismissalDate = "dismissal-date" | ||
case alert = "alert" | ||
case attributes = "attributes" | ||
case attributesType = "attributes-type" | ||
} | ||
|
||
var timestamp: Int | ||
var event: String = "start" | ||
var contentState: ContentState | ||
var dismissalDate: Int? | ||
var alert: APNSAlertNotificationContent | ||
var attributes: Attributes | ||
var attributesType: String | ||
|
||
init( | ||
timestamp: Int, | ||
contentState: ContentState, | ||
dismissalDate: Int?, | ||
alert: APNSAlertNotificationContent, | ||
attributes: Attributes, | ||
attributesType: String | ||
) { | ||
self.timestamp = timestamp | ||
self.contentState = contentState | ||
self.dismissalDate = dismissalDate | ||
self.alert = alert | ||
self.attributes = attributes | ||
self.attributesType = attributesType | ||
} | ||
} |
Oops, something went wrong.