Skip to content

Added additional error handling for invalid apns device registration ids #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions PushSharp.Apple/ApnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Net;
using PushSharp.Core;
using System.Linq;

namespace PushSharp.Apple
{
Expand Down Expand Up @@ -159,8 +160,24 @@ async Task SendBatch ()

} catch (Exception ex) {
Log.Error ("APNS-CLIENT[{0}]: Send Batch Error: Batch ID={1}, Error={2}", id, batchId, ex);
foreach (var n in toSend)
n.CompleteFailed (new ApnsNotificationException (ApnsNotificationErrorStatusCode.ConnectionError, n.Notification, ex));
var errorNotificationToSend = new List<CompletableApnsNotification> ();
//Check to see if any notification items have a bad registration id
foreach (var notificationItem in toSend) {
if (!notificationItem.Notification.IsDeviceRegistrationIdValid ())
errorNotificationToSend.Add (notificationItem);
}
if (errorNotificationToSend.Count > 0) {
//If any devices had a bad registration id assume this exception was caused by those bad ids and requeue the other notifications
foreach (var notificationItem in toSend.Except (errorNotificationToSend))
notifications.Enqueue (notificationItem);
//Report invalid token errors for each invalid registration id
foreach (var n in errorNotificationToSend)
n.CompleteFailed (new ApnsNotificationException (ApnsNotificationErrorStatusCode.InvalidToken, n.Notification, ex));
} else {
//If there were no invalid registration ids then report the errors as normal
foreach (var n in toSend)
n.CompleteFailed (new ApnsNotificationException (ApnsNotificationErrorStatusCode.ConnectionError, n.Notification, ex));
}
}

Log.Info ("APNS-Client[{0}]: Sent Batch, waiting for possible response...", id);
Expand Down
5 changes: 4 additions & 1 deletion PushSharp.Windows/WnsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public async Task Send (WnsNotification notification)

http.DefaultRequestHeaders.TryAddWithoutValidation ("X-WNS-Type", string.Format ("wns/{0}", notification.Type.ToString().ToLower ()));

if(!http.DefaultRequestHeaders.Contains("Authorization")) //prevent double values
if (notification.Priority != WnsPriority.Unspecified)
http.DefaultRequestHeaders.TryAddWithoutValidation("X-WNS-PRIORITY", ((int)notification.Priority).ToString());

if (!http.DefaultRequestHeaders.Contains("Authorization")) //prevent double values
http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + accessToken);

if (notification.RequestForStatus.HasValue)
Expand Down
2 changes: 2 additions & 0 deletions PushSharp.Windows/WnsNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract class WnsNotification : INotification

public abstract WnsNotificationType Type { get; }

public WnsPriority Priority { get; set; }

public bool IsDeviceRegistrationIdValid ()
{
return true;
Expand Down
9 changes: 9 additions & 0 deletions PushSharp.Windows/WnsNotificationStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@ public enum WnsNotificationType
Toast,
Raw
}

public enum WnsPriority
{
Unspecified = 0,
High = 1,
Meduim = 2,
Low = 3,
VeryLow = 4
}
}