public static String SendAPNSBroadCast(String message, int baget, bool mute, List <String> devices) { ApplePushChannelSettings setting = new ApplePushChannelSettings(true, KEYPATH, PASS, true); ApplePushService service = new ApplePushService(setting); AppleNotificationPayload payload = new AppleNotificationPayload(); List <String> valids = new List <string>(); List <String> invalids = new List <string>(); if (mute) { payload.Sound = ""; payload.Alert = new AppleNotificationAlert() { Body = "" }; } else { payload.Sound = "default"; payload.Alert = new AppleNotificationAlert() { Body = message }; } payload.Badge = baget; object obj = new object(); int total = 0; service.OnNotificationFailed += (object sender, INotification notification, Exception error) => { lock (obj) { total++; invalids.Add((String)notification.Tag); } Console.WriteLine("Fail / " + notification.Tag + "/ " + total); }; service.OnNotificationSent += (object sender, INotification notification) => { lock (obj) { total++; valids.Add((String)notification.Tag); } Console.WriteLine("Ok / " + total); }; foreach (var item in devices) { AppleNotification sendNotify = new AppleNotification(item, payload); sendNotify.Tag = item; service.QueueNotification(sendNotify); } while (total != devices.Count) { ; } System.Threading.Thread.Sleep(3000); service.Stop(); PushCallBack pushcallback = new APNSCallBack("iOS") { valids = valids, invalids = invalids, success = valids.Count, failure = invalids.Count, Devices = devices }; return(JsonConvert.SerializeObject(pushcallback)); }
public void TestNotifications(int toQueue, int expectSuccessful, int expectFailed, int[] idsToFail = null, bool waitForScaling = false, bool autoScale = false) { var testServer = new ApnsNodeTestServer("http://localhost:8888/"); testServer.Reset(); testServer.Setup(idsToFail ?? new int[] {}); var started = DateTime.UtcNow; int pushFailCount = 0; int pushSuccessCount = 0; AppleNotification.ResetIdentifier(); var settings = new ApplePushChannelSettings(false, appleCert, "pushsharp", true); settings.OverrideServer("localhost", 2195); settings.SkipSsl = true; settings.MillisecondsToWaitBeforeMessageDeclaredSuccess = 5000; var serviceSettings = new PushServiceSettings(); if (!autoScale) { serviceSettings.AutoScaleChannels = false; serviceSettings.Channels = 1; } var push = new ApplePushService(settings, serviceSettings); push.OnNotificationFailed += (sender, notification1, error) => { Console.WriteLine("NOTIFICATION FAILED: " + ((AppleNotification)notification1).Identifier); pushFailCount++; }; push.OnNotificationSent += (sender, notification1) => { pushSuccessCount++; }; push.OnNotificationRequeue += (sender, e) => { Console.WriteLine("REQUEUE: " + ((AppleNotification)e.Notification).Identifier); }; for (int i = 0; i < toQueue; i++) { var n = new AppleNotification("aff441e214b2b2283df799f0b8b16c17a59b7ac077e2867ea54ebf6086e55866").WithAlert("Test"); push.QueueNotification(n); } if (waitForScaling) { while (push.QueueLength > 0) { Thread.Sleep(500); } Console.WriteLine("Sleeping 3 minutes for autoscaling..."); Thread.Sleep(TimeSpan.FromMinutes(3)); Console.WriteLine("Channel Count: " + push.ChannelCount); Assert.IsTrue(push.ChannelCount <= 1); } push.Stop(); push.Dispose(); Console.WriteLine("Avg Queue Wait Time: " + push.AverageQueueWaitTime + " ms"); Console.WriteLine("Avg Send Time: " + push.AverageSendTime + " ms"); var span = DateTime.UtcNow - started; var info = testServer.GetInfo(); Console.WriteLine("Test Time: " + span.TotalMilliseconds + " ms"); Console.WriteLine("Client Failed: {0} Succeeded: {1} Sent: {2}", pushFailCount, pushSuccessCount, toQueue); Console.WriteLine("Server Failed: {0} Succeeded: {1} Received: {2} Discarded: {3}", info.FailedIds.Length, info.SuccessIds.Length, info.Received, info.Discarded); //Assert.AreEqual(toQueue, info.Received, "Server - Received Count"); Assert.AreEqual(expectFailed, info.FailedIds.Length, "Server - Failed Count"); Assert.AreEqual(expectSuccessful, info.SuccessIds.Length, "Server - Success Count"); Assert.AreEqual(expectFailed, pushFailCount, "Client - Failed Count"); Assert.AreEqual(expectSuccessful, pushSuccessCount, "Client - Success Count"); }
public void TestNotifications(int toQueue, int expectSuccessful, int expectFailed, int[] indexesToFail = null) { testPort++; int pushFailCount = 0; int pushSuccessCount = 0; int serverReceivedCount = 0; int serverReceivedFailCount = 0; int serverReceivedSuccessCount = 0; var notification = new AppleNotification("aff441e214b2b2283df799f0b8b16c17a59b7ac077e2867ea54ebf6086e55866").WithAlert("Test"); var len = notification.ToBytes().Length; var server = new TestServers.ApnsTestServer(); server.ResponseFilters.Add(new ApnsResponseFilter() { IsMatch = (identifier, token, payload) => { var id = identifier; Console.WriteLine("Server Received: id=" + id + ", payload= " + payload + ", token=" + token); if (token.StartsWith("b", StringComparison.InvariantCultureIgnoreCase)) { return(true); } return(false); }, Status = ApnsResponseStatus.InvalidToken }); var waitServerFinished = new ManualResetEvent(false); Task.Factory.StartNew(() => { try { server.Start(testPort, len, (success, identifier, token, payload) => { serverReceivedCount++; if (success) { serverReceivedSuccessCount++; } else { serverReceivedFailCount++; } }); } catch (Exception ex) { Console.WriteLine(ex); } waitServerFinished.Set(); }).ContinueWith(t => { var ex = t.Exception; Console.WriteLine(ex); }, TaskContinuationOptions.OnlyOnFaulted); var settings = new ApplePushChannelSettings(false, appleCert, "pushsharp", true); settings.OverrideServer("localhost", testPort); settings.SkipSsl = true; var push = new ApplePushService(settings, new PushServiceSettings() { AutoScaleChannels = false, Channels = 1 }); push.OnNotificationFailed += (sender, notification1, error) => pushFailCount++; push.OnNotificationSent += (sender, notification1) => pushSuccessCount++; for (int i = 0; i < toQueue; i++) { INotification n; if (indexesToFail != null && indexesToFail.Contains(i)) { n = new AppleNotification("bff441e214b2b2283df799f0b8b16c17a59b7ac077e2867ea54ebf6086e55866").WithAlert("Test"); } else { n = new AppleNotification("aff441e214b2b2283df799f0b8b16c17a59b7ac077e2867ea54ebf6086e55866").WithAlert("Test"); } push.QueueNotification(n); } push.Stop(); push.Dispose(); server.Dispose(); waitServerFinished.WaitOne(); Console.WriteLine("TEST-> DISPOSE."); Assert.AreEqual(toQueue, serverReceivedCount, "Server - Received Count"); Assert.AreEqual(expectFailed, serverReceivedFailCount, "Server - Failed Count"); Assert.AreEqual(expectSuccessful, serverReceivedSuccessCount, "Server - Success Count"); Assert.AreEqual(expectFailed, pushFailCount, "Client - Failed Count"); Assert.AreEqual(expectSuccessful, pushSuccessCount, "Client - Success Count"); }