private static async Task PostNotificationRequest(OneSignalNotificationPacket notificationPacket) { var httpClient = new HttpClient(); var content = new StringContent(JsonConvert.SerializeObject(notificationPacket), Encoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Program.Settings.NotificationAPIKey); var responseMessage = await httpClient.PostAsync("https://onesignal.com/api/v1/notifications", content); var responseBody = await responseMessage.Content.ReadAsStringAsync(); var response = JObject.Parse(responseBody); JToken errors; if (response.TryGetValue("errors", out errors)) { if (errors.Type == JTokenType.Object) { var invalidIds = errors.Value <JArray>("invalid_player_ids"); UnsubscribeDeviceTokens(invalidIds.ToObject <List <string> >()); } else { var errorMessage = $"Notification server returned following error(s): {errors}"; Program.Logger.Warn(errorMessage); } } }
private static async Task Notify(List <string> deviceTokens, string message) { var notificationPacket = new OneSignalNotificationPacket(); notificationPacket.Sound = "rohbotNotification.wav"; notificationPacket.DeviceTokens = deviceTokens; notificationPacket.Contents = new Dictionary <string, string>() { { "en", message } }; await PostNotificationRequest(notificationPacket); }
private static async Task PostNotificationRequest(OneSignalNotificationPacket notificationPacket) { var httpClient = new HttpClient(); var content = new StringContent(JsonConvert.SerializeObject(notificationPacket), Encoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Program.Settings.NotificationAPIKey); var responseMessage = await httpClient.PostAsync("https://onesignal.com/api/v1/notifications", content); var responseBody = await responseMessage.Content.ReadAsStringAsync(); dynamic response = JsonConvert.DeserializeObject(responseBody); if (response.errors != null) { var errors = ((JArray)response.errors).ToObject <List <string> >(); var errorMessage = $"Notification server returned following error(s): {string.Join(", ", errors)}"; Program.Logger.Warn(errorMessage); } }
private static async Task Notify(List <string> deviceTokens, string title, string content, string chat, long date) { var notificationPacket = new OneSignalNotificationPacket(); notificationPacket.Sound = "rohbotNotification.wav"; notificationPacket.DeviceTokens = deviceTokens; notificationPacket.Headings = new Dictionary <string, string> { { "en", title } }; notificationPacket.Contents = new Dictionary <string, string> { { "en", content } }; notificationPacket.Group = chat; notificationPacket.GroupMessage = new Dictionary <string, string> { { "en", "$[notif_count] mentions" } }; notificationPacket.Data = new Dictionary <string, string> { { "chat", chat }, { "date", date.ToString("D") } }; try { await PostNotificationRequest(notificationPacket); } catch (Exception e) { Program.Logger.Error("Failed to send push notification", e); } }
private static async Task PostNotificationRequest(OneSignalNotificationPacket notificationPacket) { var httpClient = new HttpClient(); var content = new StringContent(JsonConvert.SerializeObject(notificationPacket), Encoding.UTF8, "application/json"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Program.Settings.NotificationAPIKey); var responseMessage = await httpClient.PostAsync("https://onesignal.com/api/v1/notifications", content); var responseBody = await responseMessage.Content.ReadAsStringAsync(); dynamic response = JsonConvert.DeserializeObject(responseBody); if (response.errors != null) { var errors = ((JArray)response.errors).ToObject<List<string>>(); var errorMessage = $"Notification server returned following error(s): {string.Join(", ", errors)}"; Program.Logger.Warn(errorMessage); } }
private static async Task Notify(List<string> deviceTokens, string message) { var notificationPacket = new OneSignalNotificationPacket(); notificationPacket.Sound = "rohbotNotification.wav"; notificationPacket.DeviceTokens = deviceTokens; notificationPacket.Contents = new Dictionary<string, string>() { { "en", message } }; await PostNotificationRequest(notificationPacket); }