public async Task <TemplateNotification> InsertAsync(TemplateNotification templateNotification) { var insert = await _context.TemplateNotifications.AddAsync(templateNotification); _context.SaveChanges(); return(insert.Entity); }
public void Converter_JsonString_Valid() { TemplateNotification templateNotification = Converter.BuildTemplateNotificationFromJsonString(GetTemplatePropertiesJsonString()); Assert.NotNull(templateNotification); Assert.True(VerifyTemplate(templateNotification)); }
public static bool VerifyTemplate(TemplateNotification templateNotification) { FieldInfo templatePropertiesProperty = templateNotification.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "templateProperties"); IDictionary <string, string> actualTemplateProperties = (IDictionary <string, string>)templatePropertiesProperty.GetValue(templateNotification); return(AreTemplatePropertiesEqual(GetTemplateProperties(), actualTemplateProperties)); }
public void Converter_DictionaryTemplateProperties_Valid() { TemplateNotification templateNotification = Converter.BuildTemplateNotificationFromDictionary(GetTemplateProperties()); Assert.NotNull(templateNotification); Assert.True(VerifyTemplate(templateNotification)); }
public static void Outputs( [NotificationHub] out Notification notification, [NotificationHub(TagExpression = "tag")] out Notification notificationToTag, [NotificationHub] out WindowsNotification windowsNotification, [NotificationHub(Platform = NotificationPlatform.Wns)] out string windowsToastNotificationAsString, [NotificationHub] out TemplateNotification templateNotification, [NotificationHub] out string templateProperties, [NotificationHub] out Notification[] notificationsArray, [NotificationHub] out IDictionary <string, string> templatePropertiesDictionary, [NotificationHub] ICollector <Notification> collector, [NotificationHub] ICollector <string> collectorString, TraceWriter trace) { notification = GetTemplateNotification("Hi"); notificationToTag = GetTemplateNotification("Hi tag"); windowsNotification = new WindowsNotification(WindowsToastPayload); windowsToastNotificationAsString = WindowsToastPayload; templateNotification = GetTemplateNotification("Hello"); templateProperties = MessagePropertiesJSON; templatePropertiesDictionary = GetTemplateProperties("Hello"); notificationsArray = new TemplateNotification[] { GetTemplateNotification("Message1"), GetTemplateNotification("Message2") }; collector.Add(GetTemplateNotification("Hi")); collectorString.Add(MessagePropertiesJSON); trace.Warning("Outputs"); }
private async Task SendNotificationAsync_SendTemplateNotification_GetSuccessfulResultBack() { LoadMockData(); var notification = new TemplateNotification(new Dictionary <string, string>()); var notificationResult = await _hubClient.SendNotificationAsync(notification, "someRandomTag1 && someRandomTag2"); Assert.Equal(NotificationOutcomeState.Enqueued, notificationResult.State); RecordTestResults(); }
// This binding sends multiple push notification to any clients registered with the template // when method successfully exits. public static void SendNotificationsOnTimerTrigger( [TimerTrigger("*/30 * * * * *")] TimerInfo timerInfo, [NotificationHub] out Notification[] notifications) { notifications = new TemplateNotification[] { GetTemplateNotification("Message1"), GetTemplateNotification("Message2") }; }
private void OutputParameters( [NotificationHub] out Notification notification, [NotificationHub] out TemplateNotification templateNotification, [NotificationHub] out string templateProperties, [NotificationHub] out Notification[] notificationsArray, [NotificationHub] out IDictionary <string, string> templatePropertiesDictionary, [NotificationHub] IAsyncCollector <TemplateNotification> asyncCollector, [NotificationHub] IAsyncCollector <string> asyncCollectorString, [NotificationHub] ICollector <Notification> collector, [NotificationHub] ICollector <string> collectorString) { notification = null; templateNotification = null; templateProperties = null; templatePropertiesDictionary = null; notificationsArray = null; }
/// <summary> /// Creates a <see cref="Notification"/> from a <see cref="IPushMessage"/>. /// </summary> /// <param name="message">The <see cref="IPushMessage"/> to create the <see cref="Notification"/> from.</param> /// <returns>The resulting <see cref="Notification"/>.</returns> protected virtual Notification CreateNotification(IPushMessage message) { if (message == null) { throw new ArgumentNullException("message"); } Notification notification = null; ApplePushMessage apnsPush; WindowsPushMessage wnsPush; MpnsPushMessage mpnsPush; TemplatePushMessage templatePush; if ((wnsPush = message as WindowsPushMessage) != null) { notification = new WindowsNotification(wnsPush.ToString(), wnsPush.Headers); } else if ((mpnsPush = message as MpnsPushMessage) != null) { notification = new MpnsNotification(mpnsPush.ToString(), mpnsPush.Headers); } else if ((apnsPush = message as ApplePushMessage) != null) { DateTime?expiration = null; if (apnsPush.Expiration.HasValue) { expiration = apnsPush.Expiration.Value.DateTime; } notification = new AppleNotification(message.ToString(), expiration); } else if (message is GooglePushMessage) { notification = new GcmNotification(message.ToString()); } else if ((templatePush = message as TemplatePushMessage) != null) { notification = new TemplateNotification(templatePush); } else { throw new InvalidOperationException(GetUnknownPayloadError(message)); } return(notification); }
/// <summary> /// Sends a notification to a template registration on a notification hub. /// </summary> /// <returns></returns> public async Task <string> SendTemplateNotification(TemplateNotification notification, string tag = null) { HttpClient hc = GetClient($"messages"); if (tag != null) { hc.DefaultRequestHeaders.Add("ServiceBusNotification-Tags", tag); } try { HttpResponseMessage response = await hc.PostAsync(string.Empty, new StringContent(notification.PayLoad, Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); return(response.Headers.Location?.ToString().Replace($"{ServiceUrl}/messages/", "").Replace($"?api-version=2015-01", "")); } catch (Exception e) { throw (new Exception("Error on service call", e)); } }
internal static Notification BuildNotificationFromString(string notificationAsString, NotificationPlatform platform) { Notification notification = null; if (platform == 0) { JObject jobj = JObject.Parse(notificationAsString); Dictionary <string, string> templateProperties = jobj.ToObject <Dictionary <string, string> >(); notification = new TemplateNotification(templateProperties); } else { switch (platform) { case NotificationPlatform.Wns: notification = new WindowsNotification(notificationAsString); break; case NotificationPlatform.Apns: notification = new AppleNotification(notificationAsString); break; case NotificationPlatform.Gcm: notification = new GcmNotification(notificationAsString); break; case NotificationPlatform.Adm: notification = new AdmNotification(notificationAsString); break; case NotificationPlatform.Mpns: notification = new MpnsNotification(notificationAsString); break; } } return(notification); }
/// <summary> /// Creates a <see cref="Notification"/> from a <see cref="IPushMessage"/>. /// </summary> /// <param name="message">The <see cref="IPushMessage"/> to create the <see cref="Notification"/> from.</param> /// <returns>The resulting <see cref="Notification"/>.</returns> protected virtual Notification CreateNotification(IPushMessage message) { if (message == null) { throw new ArgumentNullException("message"); } Notification notification = null; ApplePushMessage apnsPush; WindowsPushMessage wnsPush; MpnsPushMessage mpnsPush; TemplatePushMessage templatePush; if ((wnsPush = message as WindowsPushMessage) != null) { notification = new WindowsNotification(wnsPush.ToString(), wnsPush.Headers); } else if ((mpnsPush = message as MpnsPushMessage) != null) { notification = new MpnsNotification(mpnsPush.ToString(), mpnsPush.Headers); } else if ((apnsPush = message as ApplePushMessage) != null) { DateTime? expiration = null; if (apnsPush.Expiration.HasValue) { expiration = apnsPush.Expiration.Value.DateTime; } notification = new AppleNotification(message.ToString(), expiration); } else if (message is GooglePushMessage) { notification = new GcmNotification(message.ToString()); } else if ((templatePush = message as TemplatePushMessage) != null) { notification = new TemplateNotification(templatePush); } else { throw new InvalidOperationException(GetUnknownPayloadError(message)); } return notification; }
public static void SendNotification([WebHookTrigger] string message, [NotificationHub] out Notification notification) { notification = new TemplateNotification(new Dictionary<string, string> { ["message"] = message }); }
private async void btnSend_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; btnRegistrations.Enabled = false; btnSend.Enabled = false; btnRefresh.Enabled = false; btnCreateDelete.Enabled = false; btnCancelUpdate.Enabled = false; if (notificationHubClient == null) { return; } Notification notification = null; string[] tags = null; string tagExpression = null; switch (mainTabControl.SelectedTab.Name) { case TemplateNotificationPage: var properties = NotificationInfo.TemplateProperties.ToDictionary(p => p.Name, p => p.Value); if (properties.Count > 0) { var headers = NotificationInfo.TemplateHeaders.ToDictionary(p => p.Name, p => p.Value); tags = NotificationInfo.TemplateTags.Select(t => t.Tag).ToArray(); tagExpression = txtTemplateTagExpression.Text; notification = new TemplateNotification(properties) {Headers = headers}; } else { writeToLog(NotificationPayloadCannotBeNull, false); } break; case WindowsPhoneNativeNotificationPage: if (!string.IsNullOrWhiteSpace(mpnsPayload)) { var headers = NotificationInfo.MpnsHeaders.ToDictionary(p => p.Name, p => p.Value); tags = NotificationInfo.MpnsTags.Select(t => t.Tag).ToArray(); tagExpression = txtMpnsTagExpression.Text; notification = new MpnsNotification(mpnsPayload, headers); } else { writeToLog(NotificationPayloadCannotBeNull, false); } break; case WindowsNativeNotificationPage: if (!string.IsNullOrWhiteSpace(wnsPayload)) { var headers = NotificationInfo.WnsHeaders.ToDictionary(p => p.Name, p => p.Value); tags = NotificationInfo.WnsTags.Select(t => t.Tag).ToArray(); tagExpression = txtWnsTagExpression.Text; notification = new WindowsNotification(wnsPayload, headers); } else { writeToLog(NotificationPayloadCannotBeNull, false); } break; case AppleNativeNotificationPage: if (!string.IsNullOrWhiteSpace(apnsPayload)) { var serializer = new JavaScriptSerializer(); try { serializer.Deserialize<dynamic>(apnsPayload); } catch (Exception) { writeToLog(PayloadIsNotInJsonFormat); return; } var headers = NotificationInfo.ApnsHeaders.ToDictionary(p => p.Name, p => p.Value); tags = NotificationInfo.ApnsTags.Select(t => t.Tag).ToArray(); tagExpression = txtAppleTagExpression.Text; notification = new AppleNotification(apnsPayload) { Headers = headers }; } else { writeToLog(JsonPayloadTemplateCannotBeNull, false); } break; case GoogleNativeNotificationPage: if (!string.IsNullOrWhiteSpace(gcmPayload)) { var serializer = new JavaScriptSerializer(); try { serializer.Deserialize<dynamic>(gcmPayload); } catch (Exception) { writeToLog(PayloadIsNotInJsonFormat); return; } var headers = NotificationInfo.GcmHeaders.ToDictionary(p => p.Name, p => p.Value); tags = NotificationInfo.GcmTags.Select(t => t.Tag).ToArray(); tagExpression = txtGcmTagExpression.Text; notification = new GcmNotification(gcmPayload) { Headers = headers }; } else { writeToLog(JsonPayloadTemplateCannotBeNull, false); } break; } if (notification == null) { return; } NotificationOutcome notificationOutcome; if (!string.IsNullOrWhiteSpace(tagExpression)) { notificationOutcome = await notificationHubClient.SendNotificationAsync(notification, tagExpression); WriteNotificationToLog(notification, notificationOutcome, tagExpression, tags); return; } if (tags.Any()) { notificationOutcome = await notificationHubClient.SendNotificationAsync(notification, tags); WriteNotificationToLog(notification, notificationOutcome, tagExpression, tags); return; } notificationOutcome = await notificationHubClient.SendNotificationAsync(notification); WriteNotificationToLog(notification, notificationOutcome, tagExpression, tags); } catch (Exception ex) { writeToLog(ex.Message); } finally { btnRegistrations.Enabled = true; btnSend.Enabled = true; btnRefresh.Enabled = true; btnCreateDelete.Enabled = true; btnCancelUpdate.Enabled = true; Cursor.Current = Cursors.Default; } }
private void InvalidOutputParameters( [NotificationHub] Notification notification, [NotificationHub] TemplateNotification templateNotification) { }
public static void CronJob([TimerTrigger("00:30:00")] TimerInfo timerInfo, [NotificationHub] out Notification notification) { notification = new TemplateNotification(new Dictionary<string, string> { ["message"] = "foo" }); }
public static void CronJob2([TimerTrigger("*/15 * * * * *")] TimerInfo timerInfo, [NotificationHub] out TemplateNotification notification) { notification = new TemplateNotification(new Dictionary<string, string> { ["message"] = "bar" }); }