|
| 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 | +/// An alert notification. |
| 18 | +/// |
| 19 | +/// It is **important** that you do not encode anything with the key `aps`. |
| 20 | +public struct APNSLiveActivityNotification<ContentState: Encodable>: Encodable { |
| 21 | + enum CodingKeys: CodingKey { |
| 22 | + case aps |
| 23 | + } |
| 24 | + |
| 25 | + /// The fixed content to indicate that this is a background notification. |
| 26 | + private var aps: APNSLiveActivityNotificationAPSStorage<ContentState> |
| 27 | + |
| 28 | + /// Timestamp when sending notification |
| 29 | + public var timestamp: Int { |
| 30 | + get { |
| 31 | + return self.aps.timestamp |
| 32 | + } |
| 33 | + |
| 34 | + set { |
| 35 | + self.aps.timestamp = newValue |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Event type e.g. update |
| 40 | + public var event: APNSLiveActivityNotificationEvent { |
| 41 | + get { |
| 42 | + return APNSLiveActivityNotificationEvent(rawValue: self.aps.event) |
| 43 | + } |
| 44 | + |
| 45 | + set { |
| 46 | + self.aps.event = newValue.rawValue |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// The dynamic content of a Live Activity. |
| 51 | + public var contentState: ContentState { |
| 52 | + get { |
| 53 | + return self.aps.contentState |
| 54 | + } |
| 55 | + |
| 56 | + set { |
| 57 | + self.aps.contentState = newValue |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// A canonical UUID that identifies the notification. If there is an error sending the notification, |
| 62 | + /// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits, |
| 63 | + /// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows: |
| 64 | + /// `123e4567-e89b-12d3-a456-42665544000`. |
| 65 | + /// |
| 66 | + /// If you omit this, a new UUID is created by APNs and returned in the response. |
| 67 | + public var apnsID: UUID? |
| 68 | + |
| 69 | + /// The date when the notification is no longer valid and can be discarded. If this value is not `none`, |
| 70 | + /// APNs stores the notification and tries to deliver it at least once, |
| 71 | + /// repeating the attempt as needed if it is unable to deliver the notification the first time. |
| 72 | + /// If the value is `immediately`, APNs treats the notification as if it expires immediately |
| 73 | + /// and does not store the notification or attempt to redeliver it. |
| 74 | + public var expiration: APNSNotificationExpiration |
| 75 | + |
| 76 | + /// The priority of the notification. |
| 77 | + public var priority: APNSPriority |
| 78 | + |
| 79 | + /// The topic for the notification. In general, the topic is your app’s bundle ID/app ID. |
| 80 | + public var topic: String |
| 81 | + |
| 82 | + /// Initializes a new ``APNSLiveActivityNotification``. |
| 83 | + /// |
| 84 | + /// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs. |
| 85 | + /// It is **important** that you do not encode anything with the key `aps` |
| 86 | + /// |
| 87 | + /// - Parameters: |
| 88 | + /// - expiration: The date when the notification is no longer valid and can be discarded. |
| 89 | + /// - priority: The priority of the notification. |
| 90 | + /// - appID: Your app’s bundle ID/app ID. This will be suffixed with `.push-type.liveactivity`. |
| 91 | + /// - apnsID: A canonical UUID that identifies the notification. |
| 92 | + /// - contentState: Updated content-state of live activity |
| 93 | + /// - timestamp: Timestamp when sending notification |
| 94 | + /// - event: event type e.g. update |
| 95 | + public init( |
| 96 | + expiration: APNSNotificationExpiration, |
| 97 | + priority: APNSPriority, |
| 98 | + appID: String, |
| 99 | + contentState: ContentState, |
| 100 | + event: APNSLiveActivityNotificationEvent, |
| 101 | + timestamp: Int, |
| 102 | + apnsID: UUID? = nil |
| 103 | + ) { |
| 104 | + self.init( |
| 105 | + expiration: expiration, |
| 106 | + priority: priority, |
| 107 | + topic: appID + ".push-type.liveactivity", |
| 108 | + contentState: contentState, |
| 109 | + event: event, |
| 110 | + timestamp: timestamp |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + /// Initializes a new ``APNSLiveActivityNotification``. |
| 116 | + /// |
| 117 | + /// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs. |
| 118 | + /// It is **important** that you do not encode anything with the key `aps` |
| 119 | + /// |
| 120 | + /// - Parameters: |
| 121 | + /// - expiration: The date when the notification is no longer valid and can be discarded. |
| 122 | + /// - priority: The priority of the notification. |
| 123 | + /// - topic: The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.push-type.liveactivity`. |
| 124 | + /// - apnsID: A canonical UUID that identifies the notification. |
| 125 | + /// - contentState: Updated content-state of live activity |
| 126 | + /// - timestamp: Timestamp when sending notification |
| 127 | + /// - event: event type e.g. update |
| 128 | + public init( |
| 129 | + expiration: APNSNotificationExpiration, |
| 130 | + priority: APNSPriority, |
| 131 | + topic: String, |
| 132 | + apnsID: UUID? = nil, |
| 133 | + contentState: ContentState, |
| 134 | + event: APNSLiveActivityNotificationEvent, |
| 135 | + timestamp: Int |
| 136 | + ) { |
| 137 | + self.aps = APNSLiveActivityNotificationAPSStorage( |
| 138 | + timestamp: timestamp, |
| 139 | + event: event.rawValue, |
| 140 | + contentState: contentState |
| 141 | + ) |
| 142 | + self.apnsID = apnsID |
| 143 | + self.expiration = expiration |
| 144 | + self.priority = priority |
| 145 | + self.topic = topic |
| 146 | + } |
| 147 | +} |
0 commit comments