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); } }
void InitializeWindowsBroker() { if (WnsBroker != null) { return; } if (Config.Get("PushNotification:Windows:PackageName").IsEmpty()) { return; } // Configuration var config = new WnsConfiguration(Config.Get("PushNotification:Windows:PackageName"), Config.Get("PushNotification:Windows:PackageSID"), Config.Get("PushNotification:Windows:ClientSecret")); WnsBroker = new WnsServiceBroker(config); WnsBroker.OnNotificationFailed += (notification, aggregateEx) => { aggregateEx.Handle(ex => { LogError(WnsBroker, ex); return(true); // Mark it as handled }); }; WnsBroker.Start(); }
static void InitializeWindowsBroker() { if (WnsBroker != null) { return; } if (!Config.IsDefined("PN.Windows.PackageName")) { return; } // Configuration var config = new WnsConfiguration(Config.Get <string>("PN.Windows.PackageName"), Config.Get <string>("PN.Windows.PackageSID"), Config.Get <string>("PN.Windows.ClientSecret")); WnsBroker = new WnsServiceBroker(config); WnsBroker.OnNotificationFailed += (notification, aggregateEx) => { aggregateEx.Handle(ex => { LogError(WnsBroker, ex); return(true); // Mark it as handled }); }; WnsBroker.Start(); }
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); }
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.AreEqual (attempted, succeeded); Assert.AreEqual (0, failed); }
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(); }