From b15b429557073ab26cc321dfdc327e1995cfb348 Mon Sep 17 00:00:00 2001 From: Andrew Fahlgren Date: Wed, 20 Jun 2018 16:46:19 -0700 Subject: [PATCH 1/2] Added additional error handling for invalid apns device registration ids. Now when an error occurs during connection we are checking each notification for a bad decive registration id and if we find any we requeue the notifications and send InvalidToken errors for each one. --- PushSharp.Apple/ApnsConnection.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/PushSharp.Apple/ApnsConnection.cs b/PushSharp.Apple/ApnsConnection.cs index 0908b166..30cb0e8f 100644 --- a/PushSharp.Apple/ApnsConnection.cs +++ b/PushSharp.Apple/ApnsConnection.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Net; using PushSharp.Core; +using System.Linq; namespace PushSharp.Apple { @@ -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 (); + //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); From 72e0714c2fc1b0cb4a7ab6a52328d90c3c73f9a3 Mon Sep 17 00:00:00 2001 From: Andrew Fahlgren Date: Wed, 29 Jul 2020 16:30:51 -0700 Subject: [PATCH 2/2] Added X-WNS-PRIORITY header for WNS notifications --- PushSharp.Windows/WnsConnection.cs | 5 ++++- PushSharp.Windows/WnsNotification.cs | 2 ++ PushSharp.Windows/WnsNotificationStatus.cs | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/PushSharp.Windows/WnsConnection.cs b/PushSharp.Windows/WnsConnection.cs index 6ab72c43..77ce483d 100644 --- a/PushSharp.Windows/WnsConnection.cs +++ b/PushSharp.Windows/WnsConnection.cs @@ -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) diff --git a/PushSharp.Windows/WnsNotification.cs b/PushSharp.Windows/WnsNotification.cs index a8a16dca..97afb2c7 100644 --- a/PushSharp.Windows/WnsNotification.cs +++ b/PushSharp.Windows/WnsNotification.cs @@ -14,6 +14,8 @@ public abstract class WnsNotification : INotification public abstract WnsNotificationType Type { get; } + public WnsPriority Priority { get; set; } + public bool IsDeviceRegistrationIdValid () { return true; diff --git a/PushSharp.Windows/WnsNotificationStatus.cs b/PushSharp.Windows/WnsNotificationStatus.cs index c32b62f3..d4970902 100644 --- a/PushSharp.Windows/WnsNotificationStatus.cs +++ b/PushSharp.Windows/WnsNotificationStatus.cs @@ -43,5 +43,14 @@ public enum WnsNotificationType Toast, Raw } + + public enum WnsPriority + { + Unspecified = 0, + High = 1, + Meduim = 2, + Low = 3, + VeryLow = 4 + } }