Skip to content

Commit b1a2beb

Browse files
committed
Update README.md
Fixes to the code examples.
1 parent b010788 commit b1a2beb

File tree

1 file changed

+53
-61
lines changed

1 file changed

+53
-61
lines changed

README.md

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PushSharp v4.0
33

44
PushSharp is a server-side library for sending Push Notifications to iOS/OSX (APNS), Android/Chrome (GCM), Windows/Windows Phone, Amazon (ADM) and Blackberry devices!
55

6-
PushSharp v4.0+ is a complete rewrite of the original library, aimed at taking advantage of things like async/await, HttpClient, and generally a better infrastructure using lessons learned from the old code.
6+
PushSharp v3.0+ is a complete rewrite of the original library, aimed at taking advantage of things like async/await, HttpClient, and generally a better infrastructure using lessons learned from the old code.
77

88
PushSharp will now follow [semver](http://semver.org/) versioning, so major version numbers will go up as there are any breaking api changes.
99

@@ -20,7 +20,7 @@ PushSharp will now follow [semver](http://semver.org/) versioning, so major vers
2020

2121
## Sample Usage
2222

23-
The 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.
23+
The 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.
2424

2525
### APNS Sample Usage
2626
Here is an example of how you would send an APNS notification:
@@ -30,43 +30,43 @@ Here is an example of how you would send an APNS notification:
3030
var config = new ApnsConfiguration ("push-cert.pfx", "push-cert-pwd");
3131

3232
// Create a new broker
33-
var broker = new ApnsServiceBroker (config);
33+
var apnsBroker = new ApnsServiceBroker (config);
3434

3535
// Wire up events
36-
broker.OnNotificationFailed += (notification, aggregateEx) => {
36+
apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
3737

3838
aggregateEx.Handle (ex => {
3939

4040
// See what kind of exception it was to further diagnose
4141
if (ex is ApnsNotificationException) {
42-
var notificationException = ex as ApnsNotificationException;
42+
var notificationException = (ApnsNotificationException)ex;
4343

4444
// Deal with the failed notification
4545
var apnsNotification = notificationException.Notification;
4646
var statusCode = notificationException.ErrorStatusCode;
4747

48-
Console.WriteLine ($"Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
48+
Console.WriteLine ($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
4949

5050
} else {
5151
// Inner exception might hold more useful information like an ApnsConnectionException
52-
Console.WriteLine ($"Notification Failed for some (Unknown Reason) : {ex.InnerException}");
52+
Console.WriteLine ($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
5353
}
5454

5555
// Mark it as handled
5656
return true;
5757
});
5858
};
5959

60-
broker.OnNotificationSucceeded += (notification) => {
61-
Console.WriteLine ("Notification Sent!");
60+
apnsBroker.OnNotificationSucceeded += (notification) => {
61+
Console.WriteLine ("Apple Notification Sent!");
6262
};
6363

6464
// Start the broker
65-
broker.Start ();
65+
apnsBroker.Start ();
6666

6767
foreach (var deviceToken in MY_DEVICE_TOKENS) {
6868
// Queue a notification to send
69-
broker.QueueNotification (new ApnsNotification {
69+
apnsBroker.QueueNotification (new ApnsNotification {
7070
DeviceToken = deviceToken,
7171
Payload = JObject.Parse ("{\"aps\":{\"badge\":7}}")
7272
});
@@ -75,9 +75,14 @@ foreach (var deviceToken in MY_DEVICE_TOKENS) {
7575
// Stop the broker, wait for it to finish
7676
// This isn't done after every message, but after you're
7777
// done with the broker
78-
broker.Stop ();
78+
apnsBroker.Stop ();
7979
```
8080

81+
#### Apple Notification Payload
82+
83+
More information about the payload sent in the ApnsNotification object can be found [here](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html).
84+
85+
8186
#### Apple APNS Feedback Service
8287

8388
For 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:
@@ -106,70 +111,71 @@ Here is how you would send a GCM Notification:
106111
var config = new GcmConfiguration ("GCM-SENDER-ID", "AUTH-TOKEN", null);
107112

108113
// Create a new broker
109-
var broker = new GcmServiceBroker (config);
114+
var gcmBroker = new GcmServiceBroker (config);
110115

111116
// Wire up events
112-
broker.OnNotificationFailed += (notification, aggregateEx) => {
117+
gcmBroker.OnNotificationFailed += (notification, aggregateEx) => {
113118

114119
aggregateEx.Handle (ex => {
115120

116121
// See what kind of exception it was to further diagnose
117122
if (ex is GcmNotificationException) {
118-
var notificationException = ex as GcmNotificationException;
123+
var notificationException = (GcmNotificationException)ex;
119124

120125
// Deal with the failed notification
121126
var gcmNotification = notificationException.Notification;
122127
var description = notificationException.Description;
123128

124-
Console.WriteLine ($"Notification Failed: ID={gcmNotification.MessageId}, Desc={description}");
129+
Console.WriteLine ($"GCM Notification Failed: ID={gcmNotification.MessageId}, Desc={description}");
125130
} else if (ex is GcmMulticastResultException) {
126-
127-
multicastException = ex as GcmMulticastResultException;
131+
var multicastException = (GcmMulticastResultException)ex;
128132

129133
foreach (var succeededNotification in multicastException.Succeeded) {
130-
Console.WriteLine ($"Notification Failed: ID={succeededNotification.MessageId}");
134+
Console.WriteLine ($"GCM Notification Failed: ID={succeededNotification.MessageId}");
131135
}
132136

133137
foreach (var failedKvp in multicastException.Failed) {
134138
var n = failedKvp.Key;
135139
var e = failedKvp.Value;
136140

137-
Console.WriteLine ($"Notification Failed: ID={n.MessageId}, Desc={e.Description}");
141+
Console.WriteLine ($"GCM Notification Failed: ID={n.MessageId}, Desc={e.Description}");
138142
}
139143

140-
} else if (ex is DeviceSubscriptionExpiredException) {
141-
142-
var oldId = ex.OldSubscriptionId;
143-
var newId = ex.NewSubscriptionId;
144+
} else if (ex is DeviceSubscriptonExpiredException) {
145+
var expiredException = (DeviceSubscriptonExpiredException)ex;
146+
147+
var oldId = expiredException.OldSubscriptionId;
148+
var newId = expiredException.NewSubscriptionId;
144149

145150
Console.WriteLine ($"Device RegistrationId Expired: {oldId}");
146151

147-
if (!string.IsNullOrEmpty (newId)) {
152+
if (!string.IsNullOrWhitespace (newId)) {
148153
// If this value isn't null, our subscription changed and we should update our database
149154
Console.WriteLine ($"Device RegistrationId Changed To: {newId}");
150155
}
151156
} else if (ex is RetryAfterException) {
157+
var retryException = (RetryAfterException)ex;
152158
// If you get rate limited, you should stop sending messages until after the RetryAfterUtc date
153-
Console.WriteLine ($"Rate Limited, don't send more until after {ex.RetryAfterUtc}");
159+
Console.WriteLine ($"GCM Rate Limited, don't send more until after {retryException.RetryAfterUtc}");
154160
} else {
155-
Console.WriteLine ("Notification Failed for some (Unknown Reason)");
161+
Console.WriteLine ("GCM Notification Failed for some unknown reason");
156162
}
157163

158164
// Mark it as handled
159165
return true;
160166
});
161167
};
162168

163-
broker.OnNotificationSucceeded += (notification) => {
164-
Console.WriteLine ("Notification Sent!");
169+
gcmBroker.OnNotificationSucceeded += (notification) => {
170+
Console.WriteLine ("GCM Notification Sent!");
165171
};
166172

167173
// Start the broker
168-
broker.Start ();
174+
gcmBroker.Start ();
169175

170176
foreach (var regId in MY_REGISTRATION_IDS) {
171177
// Queue a notification to send
172-
broker.QueueNotification (new GcmNotification {
178+
gcmBroker.QueueNotification (new GcmNotification {
173179
RegistrationIds = new List<string> {
174180
regId
175181
},
@@ -180,9 +186,14 @@ foreach (var regId in MY_REGISTRATION_IDS) {
180186
// Stop the broker, wait for it to finish
181187
// This isn't done after every message, but after you're
182188
// done with the broker
183-
broker.Stop ();
189+
gcmBroker.Stop ();
184190
```
185191

192+
#### Components of a GCM Notification
193+
194+
GCM notifications are much more customizable than Apple Push Notifications. More information about the messaging concepts and options can be found [here](https://developers.google.com/cloud-messaging/concept-options#components-of-a-message).
195+
196+
186197
### WNS Sample Usage
187198

188199
Here's how to send WNS Notifications:
@@ -192,55 +203,36 @@ Here's how to send WNS Notifications:
192203
var config = new WnsConfiguration ("WNS_PACKAGE_NAME", "WNS_PACKAGE_SID", "WNS_CLIENT_SECRET");
193204

194205
// Create a new broker
195-
var broker = new GcmServiceBroker (config);
206+
var wnsBroker = new WnsServiceBroker (config);
196207

197208
// Wire up events
198-
broker.OnNotificationFailed += (notification, aggregateEx) => {
209+
wnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
199210

200211
aggregateEx.Handle (ex => {
201212

202213
// See what kind of exception it was to further diagnose
203214
if (ex is WnsNotificationException) {
204-
var notificationException = ex as WnsNotificationException;
205-
206-
// Deal with the failed notification
207-
var wnsNotification = notificationException.Notification;
208-
var status = notificationException.Status;
209-
210-
Console.WriteLine ($"Notification Failed: ID={wnsNotification.ChannelUri}, Status={status}");
211-
} else if (ex is DeviceSubscriptionExpiredException) {
212-
213-
var oldId = ex.OldSubscriptionId;
214-
var newId = ex.NewSubscriptionId;
215-
216-
Console.WriteLine ($"Device RegistrationId Expired: {oldId}");
217-
218-
if (!string.IsNullOrEmpty (newId)) {
219-
// If this value isn't null, our subscription changed and we should update our database
220-
Console.WriteLine ($"Device RegistrationId Changed To: {newId}");
221-
}
222-
} else if (ex is RetryAfterException) {
223-
// If you get rate limited, you should stop sending messages until after the RetryAfterUtc date
224-
Console.WriteLine ($"Rate Limited, don't send more until after {ex.RetryAfterUtc}");
215+
var notificationException = (WnsNotificationException)ex;
216+
Console.WriteLine ($"WNS Notification Failed: {notificationException.Message}");
225217
} else {
226-
Console.WriteLine ("Notification Failed for some (Unknown Reason)");
218+
Console.WriteLine ("WNS Notification Failed for some (Unknown Reason)");
227219
}
228220

229221
// Mark it as handled
230222
return true;
231223
});
232224
};
233225

234-
broker.OnNotificationSucceeded += (notification) => {
235-
Console.WriteLine ("Notification Sent!");
226+
wnsBroker.OnNotificationSucceeded += (notification) => {
227+
Console.WriteLine ("WNS Notification Sent!");
236228
};
237229

238230
// Start the broker
239-
broker.Start ();
231+
wnsBroker.Start ();
240232

241233
foreach (var uri in MY_DEVICE_CHANNEL_URIS) {
242234
// Queue a notification to send
243-
broker.QueueNotification (new WnsToastNotification {
235+
wnsBroker.QueueNotification (new WnsToastNotification {
244236
ChannelUri = uri,
245237
Payload = XElement.Parse (@"
246238
<toast>
@@ -256,7 +248,7 @@ foreach (var uri in MY_DEVICE_CHANNEL_URIS) {
256248
// Stop the broker, wait for it to finish
257249
// This isn't done after every message, but after you're
258250
// done with the broker
259-
broker.Stop ();
251+
wnsBroker.Stop ();
260252
```
261253

262254

0 commit comments

Comments
 (0)