示例#1
0
 private void SendToWns(string token, string message)
 {
     try
     {
         var wnsBroker = new WnsServiceBroker(_wns);
         wnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
         {
             aggregateEx.Handle(ex =>
             {
                 var exception = ex as WnsNotificationException;
                 if (exception != null)
                 {
                     throw new Exception(JsonConvert.SerializeObject(new { Exception = exception.GetBaseException().Message }));
                 }
                 throw new Exception(JsonConvert.SerializeObject(new { Description = "Unknown Error Occurs !!!" }));
             });
         };
         wnsBroker.OnNotificationSucceeded += notification => { };
         wnsBroker.Start();
         wnsBroker.QueueNotification(new WnsToastNotification {
             ChannelUri = token, Payload = XElement.Parse(message)
         });
         wnsBroker.Stop();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message, e);
     }
 }
示例#2
0
        public bool Send(string messageTitle, string messageBody, IEnumerable <IUserDevice> devices)
        {
            try
            {
                InitializeBrokers();

                // Send to iOS devices
                if (ApnsBroker != null)
                {
                    devices.Where(d => d.DeviceType.Equals("iOS", caseSensitive: false)).Do(d =>
                    {
                        ApnsBroker.QueueNotification(new ApnsNotification
                        {
                            DeviceToken = d.PushNotificationToken,
                            Payload     = JObject.FromObject(new { aps = new { alert = new { title = messageTitle, body = messageBody } } })
                        });
                    });
                }

                // Send to Android devices
                if (GcmBroker != null)
                {
                    var androidDevices = devices.Where(d => d.DeviceType.Equals("Android", caseSensitive: false)).Select(d => d.PushNotificationToken).ToList();
                    if (androidDevices.Any())
                    {
                        GcmBroker.QueueNotification(new GcmNotification
                        {
                            RegistrationIds = androidDevices, // This is for multicast messages
                            Notification    = JObject.FromObject(new { body = messageBody, title = messageTitle }),
                            Data            = JObject.FromObject(new { body = messageBody, title = messageTitle })
                        });
                    }
                }

                // Send to Windows devices
                if (WnsBroker != null)
                {
                    foreach (var uri in devices.Where(d => d.DeviceType.Equals("Windows", caseSensitive: false)).Select(d => d.PushNotificationToken))
                    {
                        // Queue a notification to send
                        WnsBroker.QueueNotification(new WnsToastNotification
                        {
                            ChannelUri = uri,
                            Payload    = new XElement("toast", new XElement("visual", new XElement("binding", new XAttribute("template", "ToastText01"), new XElement("text", new XAttribute("id", "1"), messageBody))))
                        });
                    }
                }

                return(!(ApnsBroker == null && GcmBroker == null && WnsBroker == null));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                return(false);
            }
        }
        public void WNS_Send_Mutiple()
        {
            var succeeded = 0;
            var failed    = 0;
            var attempted = 0;

            var config = new WnsConfiguration(Settings.Instance.WnsPackageName,
                                              Settings.Instance.WnsPackageSid,
                                              Settings.Instance.WnsClientSecret);

            var broker = new WnsServiceBroker(config);

            broker.OnNotificationFailed += (notification, exception) =>
            {
                failed++;
            };
            broker.OnNotificationSucceeded += (notification) =>
            {
                succeeded++;
            };

            broker.Start();

            foreach (var uri in Settings.Instance.WnsChannelUris)
            {
                for (var i = 1; i <= 3; i++)
                {
                    attempted++;
                    broker.QueueNotification(new WnsToastNotification
                    {
                        ChannelUri = uri,
                        Payload    = XElement.Parse(@"
                                <toast>
                                    <visual>
                                        <binding template=""ToastText01"">
                                            <text id=""1"">WNS_Send_Multiple " + i.ToString() + @"</text>
                                        </binding>  
                                    </visual>
                                </toast>
                            ")
                    });
                }
            }

            broker.Stop();

            Assert.Equal(attempted, succeeded);
            Assert.Equal(0, failed);
        }
示例#4
0
        public void WNS_Send_Single ()
        {
            var succeeded = 0;
            var failed = 0;
            var attempted = 0;

            var config = new WnsConfiguration (Settings.Instance.WnsPackageName,
                             Settings.Instance.WnsPackageSid,
                             Settings.Instance.WnsClientSecret);

            var broker = new WnsServiceBroker (config);
            broker.OnNotificationFailed += (notification, exception) => {
                failed++;
            };
            broker.OnNotificationSucceeded += (notification) => {
                succeeded ++;
            };

            broker.Start ();

            foreach (var uri in Settings.Instance.WnsChannelUris) {
                attempted++;
                broker.QueueNotification (new WnsToastNotification {
                    ChannelUri = uri,
                    Payload = XElement.Parse (@"
                        <toast>
                            <visual>
                                <binding template=""ToastText01"">
                                    <text id=""1"">WNS_Send_Single</text>
                                </binding>  
                            </visual>
                        </toast>
                    ")
                });
            }

            broker.Stop ();

            Assert.AreEqual (attempted, succeeded);
            Assert.AreEqual (0, failed);
        }
 private void SendWp <T>(T pushMessage) where T : WnsNotification
 {
     Log.Debug("Send WP message");
     wns.QueueNotification(pushMessage);
 }
示例#6
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtDeviceToken.Text))
            {
                return;
            }

            progressBar1.Value  = 5;
            progressBar1.Value += 5;

            if (rbAndroid.Checked)
            {
                var broker = new GcmServiceBroker(new GcmConfiguration(GcmSenderId, GcmSenderAuthToken, null));

                broker.OnNotificationFailed    += BrokerOnOnNotificationFailed;
                broker.OnNotificationSucceeded += BrokerOnOnNotificationSucceeded;

                progressBar1.Value += 5;
                broker.Start();

                progressBar1.Value += 5;
                var payload = JsonConvert.SerializeObject(new GcmNotificationPayload
                {
                    Title   = txtTitle.Text,
                    Message = txtMessage.Text,
                    Badge   = txtBadge.Text,
                    JobId   = int.Parse(txtJobId.Text),
                    UserId  = int.Parse(txtUserId.Text)
                });
                broker.QueueNotification(new GcmNotification
                {
                    RegistrationIds = new List <string> {
                        txtDeviceToken.Text
                    },
                    Data = JObject.Parse(payload)
                });

                progressBar1.Value += 5;
                broker.Stop();
            }
            else if (rbiOS.Checked)
            {
                var certificateFilePath = Path.GetDirectoryName(Application.ExecutablePath) + ApnsCertificateFile;
                var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, certificateFilePath, ApnsCertificatePassword);
                var broker = new ApnsServiceBroker(config);

                broker.OnNotificationFailed    += Broker_OnNotificationFailed;
                broker.OnNotificationSucceeded += Broker_OnNotificationSucceeded;

                progressBar1.Value += 5;
                broker.Start();

                progressBar1.Value += 5;
                var payload = JsonConvert.SerializeObject(new ApnsNotificationPayload
                {
                    Aps = new Aps
                    {
                        Alert = new Alert
                        {
                            Body  = txtMessage.Text,
                            Title = txtTitle.Text
                        },
                        Badge = int.Parse(txtBadge.Text)
                    },
                    JobId  = int.Parse(txtJobId.Text),
                    UserId = int.Parse(txtUserId.Text)
                });
                broker.QueueNotification(new ApnsNotification
                {
                    DeviceToken = txtDeviceToken.Text.Replace(" ", string.Empty),
                    Payload     = JObject.Parse(payload)
                });

                progressBar1.Value += 5;
                broker.Stop();
            }
            else if (rbWindows.Checked)
            {
                var customParameters      = "/MainPage.xaml?" + HttpUtility.UrlEncode($"jobId={txtJobId.Text}&userId={txtUserId.Text}");
                var notificationXmlString = @"<toast launch='" + customParameters + $@"'>
                                              <visual lang='en-US'>
                                                <binding template='ToastImageAndText02'>
                                                  <image id='1' src='World' />
                                                  <text id='1'>{txtTitle.Text}</text>
                                                  <text id='2'>{txtMessage.Text}</text>
                                                </binding>
                                              </visual>
                                            </toast>";

                var config = new WnsConfiguration(WnsPackageName, WnsPackageSid, WnsClientSecret);
                var broker = new WnsServiceBroker(config);

                broker.OnNotificationSucceeded += BrokerOnOnNotificationSucceeded;
                broker.OnNotificationFailed    += BrokerOnOnNotificationFailed;

                progressBar1.Value += 5;
                broker.Start();

                progressBar1.Value += 5;
                broker.QueueNotification(new WnsToastNotification
                {
                    ChannelUri = txtDeviceToken.Text,
                    Payload    = XElement.Parse(notificationXmlString)
                });

                progressBar1.Value += 5;
                broker.Stop();
            }

            progressBar1.Value += 5;
            while (progressBar1.Value < 100)
            {
                progressBar1.Value += 5;
            }
            progressBar1.Value = 100;
        }
        public void SendNotification(string uri, string title, string text, string newsId)
        {
            var config    = new WnsConfiguration(package, sid, secret);
            var wnsBroker = new WnsServiceBroker(config);

            wnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
                aggregateEx.Handle(ex => {
                    // See what kind of exception it was to further diagnose
                    if (ex is WnsNotificationException)
                    {
                        var notificationException = (WnsNotificationException)ex;
                        Debug.WriteLine($"WNS Notification Failed: {notificationException.Message}");
                    }
                    else
                    {
                        Debug.WriteLine("WNS Notification Failed for some (Unknown Reason)");
                    }

                    // Mark it as handled
                    return(true);
                });
            };

            wnsBroker.OnNotificationSucceeded += (notification) => {
                Debug.WriteLine("WNS Notification Sent!");
            };

            // Start the broker
            wnsBroker.Start();
            string payload = $"<toast launch=\"{newsId}\"><visual><binding template=\"ToastGeneric\"><text>{title}</text><text>{text}</text></binding></visual></toast>";

            string tile = $@"<tile>
                               <visual>
                                    <binding template='TileMedium'>
                                        <text hint-style='subtitle'>{title}</text>
                                        <text hint-style='captionSubtle'>{text}</text>
                                    </binding>
                                    <binding template='TileWide'>
                                        <text hint-style='subtitle'>{title}</text>
                                        <text hint-style='captionSubtle'>{text}</text>
                                    </binding>
                                    <binding template='TileLarge'>
                                        <text hint-style='subtitle'>{title}</text>
                                        <text hint-style='captionSubtle'>{text}</text>
                                    </binding>
                             </visual>
                       </tile>";

            WnsToastNotification toastNotification = new WnsToastNotification
            {
                ChannelUri = uri,
                Payload    = XElement.Parse(payload)
            };

            WnsTileNotification tileNotification = new WnsTileNotification
            {
                ChannelUri = uri,
                Payload    = XElement.Parse(tile)
            };

            wnsBroker.QueueNotification(toastNotification);
            wnsBroker.QueueNotification(tileNotification);

            wnsBroker.Stop();
        }