-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Add ability to start live activities #196
Merged
kylebrowning
merged 16 commits into
swift-server-community:main
from
nakajima:start-live-activities
Jul 4, 2024
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3540be5
Add start case
nakajima 0c2bb05
Add ability to start live activities
nakajima d39bd0b
Run swift-format
nakajima e20b90a
Remove unnecessary imports
nakajima 93565bd
Add alert to live activity start notifications
nakajima 5af6d92
Remove additional conformances
nakajima fffc9ea
Remove another conformance
nakajima 3d78f52
formatting tweak
nakajima 4671d0a
Move APNSLiveActivityNotificationEvent back to a struct.
nakajima 4fb1daf
format
nakajima d2cdfb8
Oops missed this one
nakajima 2b43198
format
nakajima 3818be8
remove unrelated change
nakajima f5087ba
remove unnecessary event from struct
nakajima 2d41546
Fix indentation
nakajima a142abe
Remove unnecessary init
nakajima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
176 changes: 176 additions & 0 deletions
176
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,176 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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`. | ||
/// - apnsID: A canonical UUID that identifies the notification. | ||
/// - 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 | ||
/// - attributes: The ActivityAttributes of the live activity to start | ||
/// - attributesString: The type name of the ActivityAttributes you want to send | ||
/// - alert: An alert that will be sent along with the notification | ||
public init( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need two inits? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No I don't think we do, removed in a142abe. |
||
expiration: APNSNotificationExpiration, | ||
priority: APNSPriority, | ||
appID: String, | ||
contentState: ContentState, | ||
timestamp: Int, | ||
dismissalDate: APNSLiveActivityDismissalDate = .none, | ||
apnsID: UUID? = nil, | ||
attributes: Attributes, | ||
attributesType: String, | ||
alert: APNSAlertNotificationContent | ||
) { | ||
self.init( | ||
expiration: expiration, | ||
priority: priority, | ||
topic: appID + ".push-type.liveactivity", | ||
contentState: contentState, | ||
timestamp: timestamp, | ||
dismissalDate: dismissalDate, | ||
attributes: attributes, | ||
attributesType: attributesType, | ||
alert: alert | ||
) | ||
} | ||
|
||
/// 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. | ||
/// - topic: The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.push-type.liveactivity`. | ||
/// - apnsID: A canonical UUID that identifies the notification. | ||
/// - 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 | ||
/// - attributes: The ActivityAttributes of the live activity to start | ||
/// - attributesString: 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, | ||
topic: String, | ||
apnsID: UUID? = nil, | ||
contentState: ContentState, | ||
timestamp: Int, | ||
dismissalDate: APNSLiveActivityDismissalDate = .none, | ||
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 = topic | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just before this gets merged, funky indentation here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2d41546.