14
14
15
15
import APNSCore
16
16
import APNS
17
+ import Logging
17
18
import Foundation
18
19
20
+ let logger = Logger ( label: " APNSwiftExample " )
21
+
19
22
@available ( macOS 11 . 0 , * )
20
23
@main
21
24
struct Main {
@@ -30,7 +33,9 @@ struct Main {
30
33
static let keyIdentifier = " "
31
34
static let teamIdentifier = " "
32
35
36
+
33
37
static func main( ) async throws {
38
+
34
39
let client = APNSClient (
35
40
configuration: . init(
36
41
authenticationMethod: . jwt(
@@ -45,15 +50,21 @@ struct Main {
45
50
requestEncoder: JSONEncoder ( )
46
51
)
47
52
48
- try await Self . sendSimpleAlert ( with: client)
49
- try await Self . sendLocalizedAlert ( with: client)
50
- try await Self . sendThreadedAlert ( with: client)
51
- try await Self . sendCustomCategoryAlert ( with: client)
52
- try await Self . sendMutableContentAlert ( with: client)
53
- try await Self . sendBackground ( with: client)
54
- try await Self . sendVoIP ( with: client)
55
- try await Self . sendFileProvider ( with: client)
56
- try await Self . sendPushToTalk ( with: client)
53
+ do {
54
+ try await Self . sendSimpleAlert ( with: client)
55
+ try await Self . sendLocalizedAlert ( with: client)
56
+ try await Self . sendThreadedAlert ( with: client)
57
+ try await Self . sendCustomCategoryAlert ( with: client)
58
+ try await Self . sendMutableContentAlert ( with: client)
59
+ try await Self . sendBackground ( with: client)
60
+ try await Self . sendVoIP ( with: client)
61
+ try await Self . sendFileProvider ( with: client)
62
+ try await Self . sendPushToTalk ( with: client)
63
+ } catch {
64
+ logger. warning ( " error sending push: \( error) " )
65
+ }
66
+
67
+ try ? await client. shutdown ( )
57
68
}
58
69
}
59
70
@@ -77,6 +88,7 @@ extension Main {
77
88
) ,
78
89
deviceToken: self . deviceToken
79
90
)
91
+ logger. info ( " successfully sent simple alert notification " )
80
92
}
81
93
82
94
static func sendLocalizedAlert( with client: some APNSClientProtocol ) async throws {
@@ -95,6 +107,7 @@ extension Main {
95
107
) ,
96
108
deviceToken: self . deviceToken
97
109
)
110
+ logger. info ( " successfully sent alert localized notification " )
98
111
}
99
112
100
113
static func sendThreadedAlert( with client: some APNSClientProtocol ) async throws {
@@ -114,6 +127,7 @@ extension Main {
114
127
) ,
115
128
deviceToken: self . deviceToken
116
129
)
130
+ logger. info ( " successfully sent threaded alert " )
117
131
}
118
132
119
133
static func sendCustomCategoryAlert( with client: some APNSClientProtocol ) async throws {
@@ -133,6 +147,7 @@ extension Main {
133
147
) ,
134
148
deviceToken: self . deviceToken
135
149
)
150
+ logger. info ( " successfully sent custom category alert " )
136
151
}
137
152
138
153
static func sendMutableContentAlert( with client: some APNSClientProtocol ) async throws {
@@ -152,6 +167,7 @@ extension Main {
152
167
) ,
153
168
deviceToken: self . deviceToken
154
169
)
170
+ logger. info ( " successfully sent mutable content alert " )
155
171
}
156
172
}
157
173
@@ -168,6 +184,7 @@ extension Main {
168
184
) ,
169
185
deviceToken: self . deviceToken
170
186
)
187
+ logger. info ( " successfully sent background notification " )
171
188
}
172
189
}
173
190
@@ -185,6 +202,7 @@ extension Main {
185
202
) ,
186
203
deviceToken: self . pushKitDeviceToken
187
204
)
205
+ logger. info ( " successfully sent VoIP notification " )
188
206
}
189
207
}
190
208
@@ -201,6 +219,7 @@ extension Main {
201
219
) ,
202
220
deviceToken: self . fileProviderDeviceToken
203
221
)
222
+ logger. info ( " successfully sent FileProvider notification " )
204
223
}
205
224
}
206
225
@@ -219,5 +238,6 @@ extension Main {
219
238
) ,
220
239
deviceToken: self . ephemeralPushToken
221
240
)
241
+ logger. info ( " successfully sent Push to Talk notification " )
222
242
}
223
243
}
0 commit comments