コード例 #1
0
ファイル: ApnsTests.cs プロジェクト: oldlaurel/PushSharp
        public async Task Apns(int expectFailed, List <ApnsNotification> notifications, IEnumerable <ApnsResponseFilter> responseFilters, int batchSize = 1000, int scale = 1)
        {
            long success = 0;
            long failed  = 0;

            var server = new TestApnsServer();

            server.ResponseFilters.AddRange(responseFilters);

            // We don't want to await this, so we can start the server and listen without blocking
            server.Start();

            var config = new ApnsConfiguration("127.0.0.1", 2195)
            {
                InternalBatchSize = batchSize
            };


            var broker = new ApnsServiceBroker(config);

            broker.OnNotificationFailed += (notification, exception) => {
                Interlocked.Increment(ref failed);
            };
            broker.OnNotificationSucceeded += (notification) => Interlocked.Increment(ref success);

            broker.Start();

            if (scale != 1)
            {
                broker.ChangeScale(scale);
            }

            var c = Log.StartCounter();

            foreach (var n in notifications)
            {
                broker.QueueNotification(n);
            }

            broker.Stop();

            c.StopAndLog("Test Took {0} ms");

            await server.Stop().ConfigureAwait(false);

            var expectedSuccess = notifications.Count - expectFailed;

            var actualFailed  = failed;
            var actualSuccess = success;

            Console.WriteLine("EXPECT: Successful: {0}, Failed: {1}", expectedSuccess, expectFailed);
            Console.WriteLine("SERVER: Successful: {0}, Failed: {1}", server.Successful, server.Failed);
            Console.WriteLine("CLIENT: Successful: {0}, Failed: {1}", actualSuccess, actualFailed);

            Assert.AreEqual(expectFailed, actualFailed);
            Assert.AreEqual(expectedSuccess, actualSuccess);

            Assert.AreEqual(server.Failed, actualFailed);
            Assert.AreEqual(server.Successful, actualSuccess);
        }
コード例 #2
0
        protected override void InitializeService()
        {
            var apnsConfig = new ApnsConfiguration(
                ApnsAppConfiguration.CertificateType == "production" ? ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox,
                ApnsAppConfiguration.CertificatePath,
                ApnsAppConfiguration.CertificatePwd,
                validateIsApnsCertificate: false
                );

            _apnsServiceBroker = new ApnsServiceBroker(apnsConfig);
            _apnsServiceBroker.ChangeScale(10);
        }
コード例 #3
0
        public void sendMessage(string BarCode, string token, string WineName, int StoreId)
        {
            try
            {
                var succeeded = 0;
                var failed    = 0;
                var attempted = 0;
                logger.Info("Sending notification for :" + BarCode);
                //var config = new ApnsConfiguration (ApnsConfiguration.ApnsServerEnvironment.Sandbox, Settings.Instance.ApnsCertificateFile, Settings.Instance.ApnsCertificatePassword);
                var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, "Production2.p12", "Wineoutlet@99666");
                var broker = new ApnsServiceBroker(config);
                broker.ChangeScale(10);
                broker.OnNotificationFailed += (notification, exception) =>
                {
                    failed++;
                };
                broker.OnNotificationSucceeded += (notification) =>
                {
                    succeeded++;
                };
                broker.Start();
                string dt = token;       //"3b0d4407d7fdfb5b3c4c0f421004407d5787595b4dea0875a75db9de75d368a0";
                broker.QueueNotification(new ApnsNotification
                {
                    DeviceToken = dt,
                    //Payload = JObject.Parse("{\"aps\":{ \"alert\" : \"You've just tasted a new wine\" },{\"title\":\"222\"}")
                    //Payload = JObject.Parse("{ \"aps\" : { \"alert\" : \"You've just tasted a new wine\" } }")
                    Payload = JObject.Parse("{ \"aps\" : { \"alert\" : \"You've just tasted " + WineName + ". Please review the wine.\" },\"barcode\":\"" + BarCode + "\",\"storeid\":\"" + StoreId + "\" }")
                });
                logger.Info("Notification sent");
                //foreach (var dt in Settings.Instance.ApnsDeviceTokens)
                //{
                //    attempted++;
                //    broker.QueueNotification(new ApnsNotification
                //    {
                //        DeviceToken = dt,
                //        Payload = JObject.Parse("{ \"aps\" : { \"alert\" : \"Hello PushSharp!\" } }")
                //    });
                //}

                broker.Stop();
            }
            catch (Exception ex)
            {
                string path    = ConfigurationManager.AppSettings["ErrorLog"];
                string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                message += string.Format("Message: {0}", ex.Message);
                message += Environment.NewLine;
                message += string.Format("StackTrace: {0}", ex.StackTrace);
                message += Environment.NewLine;
                message += string.Format("Source: {0}", ex.Source);
                message += Environment.NewLine;
                message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                System.IO.Directory.CreateDirectory(path);
                using (StreamWriter writer = new StreamWriter(path + "Error.txt", true))
                {
                    writer.WriteLine(message);
                    writer.Close();
                }
            }
        }
コード例 #4
0
ファイル: ApnsTests.cs プロジェクト: Donnie888/LxServer
        public async Task Apns (int expectFailed, List<ApnsNotification> notifications, IEnumerable<ApnsResponseFilter> responseFilters, int batchSize = 1000, int scale = 1)
        {
            long success = 0;
            long failed = 0;

            var server = new TestApnsServer ();
            server.ResponseFilters.AddRange (responseFilters);

            // We don't want to await this, so we can start the server and listen without blocking
            #pragma warning disable 4014
            server.Start ();
            #pragma warning restore 4014

            var config = new ApnsConfiguration ("127.0.0.1", 2195) {
                InternalBatchSize = batchSize
            };


            var broker = new ApnsServiceBroker (config);
            broker.OnNotificationFailed += (notification, exception) => {
                Interlocked.Increment (ref failed);
            };
            broker.OnNotificationSucceeded += (notification) => Interlocked.Increment (ref success);

            broker.Start ();

            if (scale != 1)
                broker.ChangeScale (scale);

            var c = Log.StartCounter ();

            foreach (var n in notifications)
                broker.QueueNotification (n);

            broker.Stop ();

            c.StopAndLog ("Test Took {0} ms");

            await server.Stop ().ConfigureAwait (false);

            var expectedSuccess = notifications.Count - expectFailed;

            var actualFailed = failed;
            var actualSuccess = success;

            Console.WriteLine("EXPECT: Successful: {0}, Failed: {1}", expectedSuccess, expectFailed);
            Console.WriteLine("SERVER: Successful: {0}, Failed: {1}", server.Successful, server.Failed);
            Console.WriteLine("CLIENT: Successful: {0}, Failed: {1}", actualSuccess, actualFailed);

            Assert.AreEqual (expectFailed, actualFailed);
            Assert.AreEqual (expectedSuccess, actualSuccess);

            Assert.AreEqual (server.Failed, actualFailed);
            Assert.AreEqual (server.Successful, actualSuccess);
        }