public async Task Send(string homeHubId, string state, bool active, string node, string rule) { var statusMessage = new StatusMessage { Message = "StateNotification", HomeHubId = homeHubId, State = state, Active = active, Node = node, Rule = rule }; Dictionary<string, string> data = new Dictionary<string, string>() { { "message", JsonConvert.SerializeObject(statusMessage) } }; GooglePushMessage message = new GooglePushMessage(data, TimeSpan.FromHours(1)); try { var result = await _services.Push.SendAsync(message, homeHubId); _services.Log.Info(result.State.ToString()); } catch (Exception ex) { _services.Log.Error(ex.Message, null, "Push.SendAsync Error"); } }
public async Task <NotificationOutcome> SendAsync(Dictionary <string, string> data, string tag) { var jsonPayload = new GooglePushMessage(data, TimeSpan.Zero).ToString(); this.log(jsonPayload); return(await hubClient.SendGcmNativeNotificationAsync(jsonPayload, new[] { tag })); }
public static void SendPushNotification(Notification notification, ApiServices services) { try { Dictionary <string, string> data = new Dictionary <string, string>() { { "message", notification.Content } }; GooglePushMessage googleMessage = new GooglePushMessage(data, TimeSpan.FromHours(1)); var result = services.Push.SendAsync(googleMessage).Result; services.Log.Info(result.State.ToString()); data = new Dictionary <string, string>() { { "alert", notification.Content } }; ApplePushMessage appleMessage = new ApplePushMessage(notification.Content, TimeSpan.FromHours(1)); result = services.Push.SendAsync(appleMessage).Result; services.Log.Info(result.State.ToString()); } catch (Exception ex) { services.Log.Error(ex.Message, null, "Push.SendAsync Error"); } }
// POST tables/Post public async Task <IHttpActionResult> PostPost(Post item) { // Get the logged in user var currentUser = User as ServiceUser; // Set the user ID on the item item.UserId = currentUser.Id; Post current = await InsertAsync(item); Dictionary <string, string> data = new Dictionary <string, string>() { { "message", item.Text } }; GooglePushMessage message = new GooglePushMessage(data, TimeSpan.FromHours(1)); try { var result = await Services.Push.SendAsync(message); Services.Log.Info(result.State.ToString()); } catch (System.Exception ex) { Services.Log.Error(ex.Message, null, "Push.SendAsync Error"); } return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
// POST tables/Smartphone public async Task <IHttpActionResult> PostSmartphone(Smartphone smartphone) { Smartphone current = await InsertAsync(smartphone); // Android (Google) var datosGoogle = new Dictionary <string, string>() { { "mensaje", JsonConvert.SerializeObject(smartphone) } }; var mensajeGoogle = new GooglePushMessage(datosGoogle, TimeSpan.FromHours(1)); // UWP var datosUwp = @"<?xml version=""1.0"" encoding=""utf - 8""?> <toast> <visual> <binding template=""ToastText01""> <text id=""1"">Nuevo smartphone</text> <text id=""1"">Modelo: " + smartphone.Modelo + @"</text> <text id=""1"">Fabricante: " + smartphone.Fabricante + @"</text> </binding> </visual> <actions> <action content=""Ver"" arguments=""check"" /> <action content=""Descartar"" arguments=""cancel""/> </actions> </toast>"; var mensajeUwp = new WindowsPushMessage() { XmlPayload = datosUwp }; // Tag var tag = "NuevoSmartphone"; var tags = new List <string>(); tags.Add(tag); try { // Android (Google) Push var resultadoGoogle = await Services.Push.SendAsync(mensajeGoogle, tags); Services.Log.Info("Google - " + resultadoGoogle.State); // UWP Push var resultadoUwp = await Services.Push.SendAsync(mensajeUwp, tags); Services.Log.Info("Microsoft - " + resultadoUwp.State); } catch (Exception e) { Services.Log.Error(e.Message); } return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
public async Task <HttpResponseMessage> Post() { var data = await this.Request.Content.ReadAsAsync <JObject>(); var method = (string)data["method"]; if (method == null) { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } if (method == "send") { var serialize = new JsonSerializer(); var token = (string)data["token"]; var payload = (JObject)data["payload"]; var type = (string)data["type"]; if (payload == null || token == null) { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } if (type == "template") { TemplatePushMessage message = new TemplatePushMessage(); var keys = payload.Properties(); foreach (JProperty key in keys) { Services.Log.Info("Key: " + key.Name); message.Add(key.Name, (string)key.Value); } var result = await Services.Push.SendAsync(message, "World"); } else if (type == "gcm") { GooglePushMessage message = new GooglePushMessage(); message.JsonPayload = payload.ToString(); var result = await Services.Push.SendAsync(message); } else { ApplePushMessage message = new ApplePushMessage(); Services.Log.Info(payload.ToString()); message.JsonPayload = payload.ToString(); var result = await Services.Push.SendAsync(message); } } else { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } return(new HttpResponseMessage(System.Net.HttpStatusCode.OK)); }
public static async void SendGooglePush(ApiServices service, string message) { Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("message", message); GooglePushMessage googlePushMessage = new GooglePushMessage(data, TimeSpan.FromHours(1)); SendPush(service, googlePushMessage); }
// POST tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959 public async Task <IHttpActionResult> PostTodoItem(TodoItem item) { TodoItem current = await InsertAsync(item); var message = new GooglePushMessage(new Dictionary <string, string> { { "message", "Hello from Web API!" } }, TimeSpan.FromDays(1)); var result = await Services.Push.SendAsync(message); Services.Log.Info(result.State.ToString()); return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
public void Serializes_Demo() { // Arrange GooglePushMessage gcmNot = new GooglePushMessage(); gcmNot.CollapseKey = "demo"; gcmNot.DelayWhileIdle = true; gcmNot.Data.Add("key1", "value1"); gcmNot.Data.Add("key2", "value2"); gcmNot.TimeToLiveInSeconds = 3; // Act string actual = gcmNot.ToString(); // Assert Assert.Equal(Templates["Demo"], actual); }
private async Task SendNotification(string message) { try { #region Windows push notification // Create a WNS native toast. WindowsPushMessage windowsPushMessage = new WindowsPushMessage { XmlPayload = @"<?xml version=""1.0"" encoding=""utf-8""?>" + @"<toast><visual><binding template=""ToastText02"">" + @"<text id=""1"">Blog post</text>" + @"<text id=""2"">" + message + @"</text>" + @"</binding></visual></toast>" }; // Define the XML paylod for a WNS native toast notification // that contains the text of the inserted item. var resultWindows = await Services.Push.SendAsync(windowsPushMessage); //await Services.Push.SendAsync(windowsPushMessage,"azure mobile services"); Services.Log.Info(resultWindows.State.ToString()); #endregion #region Google Push notification GooglePushMessage googlePushMessage = new GooglePushMessage(); googlePushMessage.CollapseKey = "Blog post"; googlePushMessage.Data.Add("message", message); googlePushMessage.DelayWhileIdle = true; googlePushMessage.TimeToLiveInSeconds = TimeSpan.FromHours(1).Seconds; var resultGoogle = await Services.Push.SendAsync(googlePushMessage); Services.Log.Info(resultGoogle.State.ToString()); #endregion } catch (System.Exception ex) { Services.Log.Error(ex.Message, null, "Push.SendAsync Error"); } }
public void GooglePushMessage_DataTimeToLive_SetsDataAndTimeToLive(IDictionary<string, string> data, TimeSpan? timeToLive) { // Act GooglePushMessage gcmNot = new GooglePushMessage(data, timeToLive); // Assert foreach (string dataKey in data.Keys) { Assert.Equal(data[dataKey], gcmNot.Data[dataKey]); } if (timeToLive != null) { Assert.Equal(timeToLive.Value.TotalSeconds, gcmNot.TimeToLiveInSeconds.Value); } else { Assert.Null(gcmNot.TimeToLiveInSeconds); } }
private async Task sendPushNotification(Post item, ServiceUser currentUser) { Dictionary <string, string> data = new Dictionary <string, string>() { { "message", item.Text } }; GooglePushMessage message = new GooglePushMessage(data, TimeSpan.FromHours(1)); try { //var result = await Services.Push.SendAsync(message); // Use a tag to only send the notification to the logged-in user. var result = await Services.Push.SendAsync(message, currentUser.Id); Services.Log.Info(result.State.ToString()); } catch (System.Exception ex) { Services.Log.Error(ex.Message, null, "Push.SendAsync Error"); } }
public static AutomaticNotification SendPushNotification(CurrencyRate rate, NotificationType notificationType, ApiServices services) { try { var now = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Argentina Standard Time")); var category = notificationType == NotificationType.Hourly ? "Hourly" : "OpenClose"; var msj = notificationType == NotificationType.Hourly ? string.Empty : notificationType == NotificationType.OpenMarket ? "Apertura " : "Cierre "; msj += rate.Name + ": $" + decimal.Round(rate.Buy, 2, MidpointRounding.AwayFromZero) + " / $" + decimal.Round(rate.Sell, 2, MidpointRounding.AwayFromZero); var automaticNotification = new AutomaticNotification { Id = Guid.NewGuid().ToString(), NotificationType = notificationType, DateSent = now, Content = msj }; Dictionary <string, string> data = new Dictionary <string, string>() { { "message", msj }, { "category", category } }; GooglePushMessage googleMessage = new GooglePushMessage(data, TimeSpan.FromHours(1)); var result = services.Push.SendAsync(googleMessage, category).Result; services.Log.Info(result.State.ToString()); ApplePushMessage appleMessage = new ApplePushMessage(msj, TimeSpan.FromHours(1)); result = services.Push.SendAsync(appleMessage, category == "OpenClose" ? "OpenAndClose" : category).Result; services.Log.Info(result.State.ToString()); return(automaticNotification); } catch (Exception ex) { services.Log.Error(ex.Message, null, "Push.SendAsync Error"); return(null); } }
public async Task <HttpResponseMessage> Post() { var data = await this.Request.Content.ReadAsAsync <JObject>(); var method = (string)data["method"]; if (method == null) { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } if (method == "send") { var serialize = new JsonSerializer(); var token = (string)data["token"]; if (data["payload"] == null || token == null) { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } // Payload could be a string or a dictionary var payloadString = data["payload"].ToString(); var type = (string)data["type"]; var tag = (string)data["tag"]; if (type == "template") { TemplatePushMessage message = new TemplatePushMessage(); var payload = JObject.Parse(payloadString); var keys = payload.Properties(); foreach (JProperty key in keys) { this.traceWriter.Info("Key: " + key.Name); message.Add(key.Name, (string)key.Value); } var result = await this.pushClient.SendAsync(message, "World"); } else if (type == "gcm") { GooglePushMessage message = new GooglePushMessage(); message.JsonPayload = payloadString; var result = await this.pushClient.SendAsync(message); } else if (type == "apns") { ApplePushMessage message = new ApplePushMessage(); this.traceWriter.Info(payloadString.ToString()); message.JsonPayload = payloadString.ToString(); var result = await this.pushClient.SendAsync(message); } else if (type == "wns") { var wnsType = (string)data["wnsType"]; WindowsPushMessage message = new WindowsPushMessage(); message.XmlPayload = payloadString; message.Headers.Add("X-WNS-Type", type + '/' + wnsType); if (tag != null) { await this.pushClient.SendAsync(message, tag); } else { await this.pushClient.SendAsync(message); } } } else { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } return(new HttpResponseMessage(System.Net.HttpStatusCode.OK)); }
public async Task<NotificationOutcome> SendAsync(Dictionary<string, string> data, string tag) { var jsonPayload = new GooglePushMessage(data, TimeSpan.Zero).ToString(); this.log(jsonPayload); return await hubClient.SendGcmNativeNotificationAsync(jsonPayload, new[] { tag }); }
public async Task<HttpResponseMessage> Post() { var data = await this.Request.Content.ReadAsAsync<JObject>(); var method = (string)data["method"]; if (method == null) { return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); } if (method == "send") { var serialize = new JsonSerializer(); var token = (string)data["token"]; if (data["payload"] == null || token == null) { return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); } // Payload could be a string or a dictionary var payloadString = data["payload"].ToString(); var type = (string)data["type"]; var tag = (string)data["tag"]; if (type == "template") { TemplatePushMessage message = new TemplatePushMessage(); var payload = JObject.Parse(payloadString); var keys = payload.Properties(); foreach (JProperty key in keys) { this.traceWriter.Info("Key: " + key.Name); message.Add(key.Name, (string)key.Value); } var result = await this.pushClient.SendAsync(message); } else if (type == "gcm") { GooglePushMessage message = new GooglePushMessage(); message.JsonPayload = payloadString; var result = await this.pushClient.SendAsync(message); } else if (type == "apns") { ApplePushMessage message = new ApplePushMessage(); this.traceWriter.Info(payloadString.ToString()); message.JsonPayload = payloadString.ToString(); var result = await this.pushClient.SendAsync(message); } else if (type == "wns") { var wnsType = (string)data["wnsType"]; WindowsPushMessage message = new WindowsPushMessage(); message.XmlPayload = payloadString; message.Headers.Add("X-WNS-Type", type + '/' + wnsType); if (tag != null) { await this.pushClient.SendAsync(message, tag); } else { await this.pushClient.SendAsync(message); } } } else { return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); } return new HttpResponseMessage(System.Net.HttpStatusCode.OK); }
public void JsonPayload_TakesOverSerialization() { // Arrange GooglePushMessage gcmNot = new GooglePushMessage() { JsonPayload = "text" }; // Act string actual = gcmNot.ToString(); // Assert Assert.Equal("text", actual); }