Skip to content
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
17 changes: 8 additions & 9 deletions Sources/APNSCore/LiveActivity/APNSLiveActivityNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,34 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
get {
return self.aps.timestamp
}

set {
self.aps.timestamp = newValue
}
}

/// Event type e.g. update
public var event: APNSLiveActivityNotificationEvent {
get {
return APNSLiveActivityNotificationEvent(rawValue: self.aps.event)
}

set {
self.aps.event = newValue.rawValue
}
}

/// 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)
Expand All @@ -66,7 +66,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
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:
Expand Down Expand Up @@ -123,7 +123,6 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
dismissalDate: dismissalDate
)
}


/// Initializes a new ``APNSLiveActivityNotification``.
///
Expand Down Expand Up @@ -152,7 +151,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage
) {
self.aps = APNSLiveActivityNotificationAPSStorage(
timestamp: timestamp,
event: event.rawValue,
event: event.rawValue,

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2d41546.

contentState: contentState,
dismissalDate: dismissalDate.dismissal
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ struct APNSLiveActivityNotificationAPSStorage<ContentState: Encodable>: Encodabl
var event: String
var contentState: ContentState
var dismissalDate: Int?

init(
timestamp: Int,
event: String,
contentState: ContentState,
dismissalDate: Int?
) {
self.timestamp = timestamp
self.event = event
self.contentState = contentState
self.dismissalDate = dismissalDate
self.event = event
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
//===----------------------------------------------------------------------===//

public struct APNSLiveActivityNotificationEvent: Hashable {

/// The underlying raw value that is send to APNs.
@usableFromInline
internal let rawValue: String

/// Specifies that live activity should be updated
public static let update = Self(rawValue: "update")

/// Specifies that live activity should be ended
public static let end = Self(rawValue: "end")
}
176 changes: 176 additions & 0 deletions Sources/APNSCore/LiveActivity/APNSStartLiveActivityNotification.swift
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need two inits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
}
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
}
}
Loading