1414
1515import APNSCore
1616import APNS
17+ import Logging
1718import Foundation
1819
20+ let logger = Logger ( label: " APNSwiftExample " )
21+
1922@available ( macOS 11 . 0 , * )
2023@main
2124struct Main {
@@ -30,7 +33,9 @@ struct Main {
3033 static let keyIdentifier = " "
3134 static let teamIdentifier = " "
3235
36+
3337 static func main( ) async throws {
38+
3439 let client = APNSClient (
3540 configuration: . init(
3641 authenticationMethod: . jwt(
@@ -45,15 +50,21 @@ struct Main {
4550 requestEncoder: JSONEncoder ( )
4651 )
4752
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 ( )
5768 }
5869}
5970
@@ -77,6 +88,7 @@ extension Main {
7788 ) ,
7889 deviceToken: self . deviceToken
7990 )
91+ logger. info ( " successfully sent simple alert notification " )
8092 }
8193
8294 static func sendLocalizedAlert( with client: some APNSClientProtocol ) async throws {
@@ -95,6 +107,7 @@ extension Main {
95107 ) ,
96108 deviceToken: self . deviceToken
97109 )
110+ logger. info ( " successfully sent alert localized notification " )
98111 }
99112
100113 static func sendThreadedAlert( with client: some APNSClientProtocol ) async throws {
@@ -114,6 +127,7 @@ extension Main {
114127 ) ,
115128 deviceToken: self . deviceToken
116129 )
130+ logger. info ( " successfully sent threaded alert " )
117131 }
118132
119133 static func sendCustomCategoryAlert( with client: some APNSClientProtocol ) async throws {
@@ -133,6 +147,7 @@ extension Main {
133147 ) ,
134148 deviceToken: self . deviceToken
135149 )
150+ logger. info ( " successfully sent custom category alert " )
136151 }
137152
138153 static func sendMutableContentAlert( with client: some APNSClientProtocol ) async throws {
@@ -152,6 +167,7 @@ extension Main {
152167 ) ,
153168 deviceToken: self . deviceToken
154169 )
170+ logger. info ( " successfully sent mutable content alert " )
155171 }
156172}
157173
@@ -168,6 +184,7 @@ extension Main {
168184 ) ,
169185 deviceToken: self . deviceToken
170186 )
187+ logger. info ( " successfully sent background notification " )
171188 }
172189}
173190
@@ -185,6 +202,7 @@ extension Main {
185202 ) ,
186203 deviceToken: self . pushKitDeviceToken
187204 )
205+ logger. info ( " successfully sent VoIP notification " )
188206 }
189207}
190208
@@ -201,6 +219,7 @@ extension Main {
201219 ) ,
202220 deviceToken: self . fileProviderDeviceToken
203221 )
222+ logger. info ( " successfully sent FileProvider notification " )
204223 }
205224}
206225
@@ -219,5 +238,6 @@ extension Main {
219238 ) ,
220239 deviceToken: self . ephemeralPushToken
221240 )
241+ logger. info ( " successfully sent Push to Talk notification " )
222242 }
223243}
0 commit comments