@@ -20,7 +20,9 @@ PushSharp v3.0 is a complete rewrite of the original library, aimed at taking ad
2020
2121The API in v3.x series is quite different from 2.x. The goal is to simplify things and focus on the core functionality of the library, leaving things like constructing valid payloads up to the developer.
2222
23+ ### APNS Sample Usage
2324Here is an example of how you would send an APNS notification:
25+
2426``` csharp
2527// Configuration
2628var config = new ApnsConfiguration (" push-cert.pfx" , " push-cert-pwd" );
@@ -35,18 +37,17 @@ broker.OnNotificationFailed += (notification, aggregateEx) => {
3537
3638 // See what kind of exception it was to further diagnose
3739 if (ex is ApnsNotificationException ) {
38- var apnsEx = ex as ApnsNotificationException ;
39-
40+ var notificationException = ex as ApnsNotificationException ;
41+
4042 // Deal with the failed notification
41- var n = apnsEx .Notification ;
42-
43- Console .WriteLine ($" Notification Failed: ID={n .Identifier }, Code={apnsEx .ErrorStatusCode }" );
43+ var apnsNotification = notificationException .Notification ;
44+ var statusCode = notificationException .ErrorStatusCode ;
45+
46+ Console .WriteLine ($" Notification Failed: ID={apnsNotification .Identifier }, Code={statusCode }" );
4447
45- } else if (ex is ApnsConnectionException ) {
46- // Something failed while connecting (maybe bad cert?)
47- Console .WriteLine (" Notification Failed (Bad APNS Connection)!" );
4848 } else {
49- Console .WriteLine (" Notification Failed (Unknown Reason)!" );
49+ // Inner exception might hold more useful information like an ApnsConnectionException
50+ Console .WriteLine ($" Notification Failed for some (Unknown Reason) : {ex .InnerException }" );
5051 }
5152
5253 // Mark it as handled
@@ -61,21 +62,21 @@ broker.OnNotificationSucceeded += (notification) => {
6162// Start the broker
6263broker .Start ();
6364
64- // Queue a notification to send
65- broker .QueueNotification (new ApnsNotification {
66- DeviceToken = " device-token-from-device" ,
65+ foreach (var deviceToken in MY_DEVICE_TOKENS ) {
66+ // Queue a notification to send
67+ broker .QueueNotification (new ApnsNotification {
68+ DeviceToken = deviceToken ,
6769 Payload = JObject .Parse (" {\" aps\" :{\" badge\" :7}}" )
6870 });
71+ }
6972
7073// Stop the broker, wait for it to finish
7174// This isn't done after every message, but after you're
7275// done with the broker
7376broker .Stop ();
7477```
7578
76- Other platforms are structured the same way, although the configuration of each broker may vary.
77-
78- ### Apple APNS Feedback Service
79+ #### Apple APNS Feedback Service
7980
8081For APNS you will also need to occasionally check with the feedback service to see if there are any expired device tokens you should no longer send notifications to. Here's an example of how you would do that:
8182
@@ -94,6 +95,169 @@ fbs.Check ();
9495```
9596
9697
98+ ### GCM Sample Usage
99+
100+ Here is how you would send a GCM Notification:
101+
102+ ``` csharp
103+ // Configuration
104+ var config = new GcmConfiguration (" GCM-SENDER-ID" , " AUTH-TOKEN" , null );
105+
106+ // Create a new broker
107+ var broker = new GcmServiceBroker (config );
108+
109+ // Wire up events
110+ broker .OnNotificationFailed += (notification , aggregateEx ) => {
111+
112+ aggregateEx .Handle (ex => {
113+
114+ // See what kind of exception it was to further diagnose
115+ if (ex is GcmNotificationException ) {
116+ var notificationException = ex as GcmNotificationException ;
117+
118+ // Deal with the failed notification
119+ var gcmNotification = notificationException .Notification ;
120+ var description = notificationException .Description ;
121+
122+ Console .WriteLine ($" Notification Failed: ID={gcmNotification .MessageId }, Desc={description }" );
123+ } else if (ex is GcmMulticastResultException ) {
124+
125+ multicastException = ex as GcmMulticastResultException ;
126+
127+ foreach (var succeededNotification in multicastException .Succeeded ) {
128+ Console .WriteLine ($" Notification Failed: ID={succeededNotification .MessageId }" );
129+ }
130+
131+ foreach (var failedKvp in multicastException .Failed ) {
132+ var n = failedKvp .Key ;
133+ var e = failedKvp .Value ;
134+
135+ Console .WriteLine ($" Notification Failed: ID={n .MessageId }, Desc={e .Description }" );
136+ }
137+
138+ } else if (ex is DeviceSubscriptionExpiredException ) {
139+
140+ var oldId = ex .OldSubscriptionId ;
141+ var newId = ex .NewSubscriptionId ;
142+
143+ Console .WriteLine ($" Device RegistrationId Expired: {oldId }" );
144+
145+ if (! string .IsNullOrEmpty (newId )) {
146+ // If this value isn't null, our subscription changed and we should update our database
147+ Console .WriteLine ($" Device RegistrationId Changed To: {newId }" );
148+ }
149+ } else if (ex is RetryAfterException ) {
150+ // If you get rate limited, you should stop sending messages until after the RetryAfterUtc date
151+ Console .WriteLine ($" Rate Limited, don't send more until after {ex .RetryAfterUtc }" );
152+ } else {
153+ Console .WriteLine (" Notification Failed for some (Unknown Reason)" );
154+ }
155+
156+ // Mark it as handled
157+ return true ;
158+ });
159+ };
160+
161+ broker .OnNotificationSucceeded += (notification ) => {
162+ Console .WriteLine (" Notification Sent!" );
163+ };
164+
165+ // Start the broker
166+ broker .Start ();
167+
168+ foreach (var regId in MY_REGISTRATION_IDS ) {
169+ // Queue a notification to send
170+ broker .QueueNotification (new GcmNotification {
171+ RegistrationIds = new List <string > {
172+ regId
173+ },
174+ Data = JObject .Parse (" { \" somekey\" : \" somevalue\" }" )
175+ });
176+ }
177+
178+ // Stop the broker, wait for it to finish
179+ // This isn't done after every message, but after you're
180+ // done with the broker
181+ broker .Stop ();
182+ ```
183+
184+ ### WNS Sample Usage
185+
186+ Here's how to send WNS Notifications:
187+
188+ ``` csharp
189+ // Configuration
190+ var config = new WnsConfiguration (" WNS_PACKAGE_NAME" , " WNS_PACKAGE_SID" , " WNS_CLIENT_SECRET" );
191+
192+ // Create a new broker
193+ var broker = new GcmServiceBroker (config );
194+
195+ // Wire up events
196+ broker .OnNotificationFailed += (notification , aggregateEx ) => {
197+
198+ aggregateEx .Handle (ex => {
199+
200+ // See what kind of exception it was to further diagnose
201+ if (ex is WnsNotificationException ) {
202+ var notificationException = ex as WnsNotificationException ;
203+
204+ // Deal with the failed notification
205+ var wnsNotification = notificationException .Notification ;
206+ var status = notificationException .Status ;
207+
208+ Console .WriteLine ($" Notification Failed: ID={wnsNotification .ChannelUri }, Status={status }" );
209+ } else if (ex is DeviceSubscriptionExpiredException ) {
210+
211+ var oldId = ex .OldSubscriptionId ;
212+ var newId = ex .NewSubscriptionId ;
213+
214+ Console .WriteLine ($" Device RegistrationId Expired: {oldId }" );
215+
216+ if (! string .IsNullOrEmpty (newId )) {
217+ // If this value isn't null, our subscription changed and we should update our database
218+ Console .WriteLine ($" Device RegistrationId Changed To: {newId }" );
219+ }
220+ } else if (ex is RetryAfterException ) {
221+ // If you get rate limited, you should stop sending messages until after the RetryAfterUtc date
222+ Console .WriteLine ($" Rate Limited, don't send more until after {ex .RetryAfterUtc }" );
223+ } else {
224+ Console .WriteLine (" Notification Failed for some (Unknown Reason)" );
225+ }
226+
227+ // Mark it as handled
228+ return true ;
229+ });
230+ };
231+
232+ broker .OnNotificationSucceeded += (notification ) => {
233+ Console .WriteLine (" Notification Sent!" );
234+ };
235+
236+ // Start the broker
237+ broker .Start ();
238+
239+ foreach (var uri in MY_DEVICE_CHANNEL_URIS ) {
240+ // Queue a notification to send
241+ broker .QueueNotification (new WnsToastNotification {
242+ ChannelUri = uri ,
243+ Payload = XElement .Parse (@"
244+ <toast>
245+ <visual>
246+ <binding template="" ToastText01"" >
247+ <text id="" 1"" >WNS_Send_Single</text>
248+ </binding>
249+ </visual>
250+ </toast>" )
251+ });
252+ }
253+
254+ // Stop the broker, wait for it to finish
255+ // This isn't done after every message, but after you're
256+ // done with the broker
257+ broker .Stop ();
258+ ```
259+
260+
97261## How to Migrate from PushSharp 2.x to 3.x
98262
99263Please see this Wiki page for more information: https://github.com/Redth/PushSharp/wiki/Migrating-from-PushSharp-2.x-to-3.x
0 commit comments