|
| 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 | +} |
0 commit comments