Пример #1
0
        public async Task SendPushNotifications(PushContent pushContent)
        {
            var appCenterPushUrl = "https://api.appcenter.ms/v0.1/apps/DotNetRu/{0}/push/notifications";

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("X-API-Token", pushSettings.AppCenterApiToken);

            var appCenterPushNotification = new AppCenterPushNotification
            {
                NotificationContent = new NotificationContent()
                {
                    Name  = "Conference update",
                    Body  = pushContent.Body,
                    Title = pushContent.Title
                },
                NotificationTarget = null
            };

            foreach (var app in pushSettings.AppCenterAppNames)
            {
                var push = await httpClient.PostAsJsonAsync(string.Format(appCenterPushUrl, app), appCenterPushNotification);

                push.EnsureSuccessStatusCode();
            }
        }
Пример #2
0
        public HttpResponseMessage Push(PushContent content)
        {
            using (var client = new HttpClient())
            {
                var stringContent = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

                var response = client.PostAsync(_url, stringContent).Result;

                return(response.EnsureSuccessStatusCode());
            }
        }
Пример #3
0
        public HttpResponseMessage Push(PushContent content)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_url);

                var data = JsonConvert.SerializeObject(content);

                var payload = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("info", data)
                });

                var response = client.PostAsync("im/eyesFreeLog", payload).Result;

                return(response.EnsureSuccessStatusCode());
            }
        }
Пример #4
0
        public async Task <string> Push(string title, string message, Dictionary <string, string> customData = default)
        {
            try
            {
                if (title.Length > 100)
                {
                    title = title.Substring(0, 95) + "...";
                }

                if (message.Length > 100)
                {
                    message = message.Substring(0, 95) + "...";
                }

                if (!receiver.IOSDevices.Any() && !receiver.AndroidDevices.Any() && !receiver.AndroidStaffDevices.Any() && !receiver.IOSStaffDevices.Any())
                {
                    return(null); //No devices to send
                }
                //To make sure in Android, title and message is retain when click from notification. Else it's lost when app is in background
                if (customData == null)
                {
                    customData = new Dictionary <string, string>();
                }

                if (!customData.ContainsKey("Title"))
                {
                    customData.Add("Title", title);
                }

                if (!customData.ContainsKey("Message"))
                {
                    customData.Add("Message", message);
                }

                //custom data cannot exceed 100 char
                foreach (string key in customData.Keys)
                {
                    if (customData[key].Length > 100)
                    {
                        customData[key] = customData[key].Substring(0, 95) + "...";
                    }
                }

                var push = new PushContent
                {
                    Content = new Content
                    {
                        Title      = title,
                        Body       = message,
                        CustomData = customData
                    },
                    Target = new Target
                    {
                        Type = Constants.DeviceTarget // "devices_target"
                    }
                };

                HttpClient httpClient = new HttpClient();

                //Set the content header to json and inject the token
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Add(Constants.ApiKeyName, Constants.FullAccessToken);

                //Needed to solve SSL/TLS issue when
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                var resultString = string.Empty;

                if (receiver.IOSDevices.Any())
                {
                    //receiver.IOSDevices.Add("77a6557d-f528-412d-b9d2-a5435b456c3e");
                    //receiver.IOSDevices.Add("43a4181d-160a-44de-ad18-4514e709e78e");
                    push.Target.Devices = receiver.IOSDevices;

                    string content = JsonConvert.SerializeObject(push);

                    HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");

                    string URL = $"{Constants.Url}/{Constants.Organization}/{Constants.AppNameiOS}/{Constants.Apis.Notification}";

                    var result = await httpClient.PostAsync(URL, httpContent);

                    resultString += await result.Content.ReadAsStringAsync();
                }

                if (receiver.AndroidDevices.Any())
                {
                    //receiver.AndroidDevices.Add("3b49b6a6-724a-4a70-9114-0bded38dbea6");
                    //receiver.AndroidDevices.Add("da67bf91-c6c6-485a-9002-08279505cb25");
                    push.Target.Devices = receiver.AndroidDevices;

                    string content = JsonConvert.SerializeObject(push);

                    HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");

                    string URL = $"{Constants.Url}/{Constants.Organization}/{Constants.AppNameAndroid}/{Constants.Apis.Notification}";

                    var result = await httpClient.PostAsync(URL, httpContent);

                    resultString += await result.Content.ReadAsStringAsync();
                }

                if (receiver.AndroidStaffDevices.Any())
                {
                    push.Target.Devices = receiver.AndroidStaffDevices;
                    string      content     = JsonConvert.SerializeObject(push);
                    HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");
                    string      URL         = $"{Constants.Url}/{Constants.Organization}/{Constants.AppNameStaffAndroid}/{Constants.Apis.Notification}";
                    var         result      = await httpClient.PostAsync(URL, httpContent);

                    resultString += await result.Content.ReadAsStringAsync();
                }

                if (receiver.IOSStaffDevices.Any())
                {
                    push.Target.Devices = receiver.IOSStaffDevices;
                    string      content     = JsonConvert.SerializeObject(push);
                    HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");
                    string      URL         = $"{Constants.Url}/{Constants.Organization}/{Constants.AppNameStaffiOS}/{Constants.Apis.Notification}";
                    var         result      = await httpClient.PostAsync(URL, httpContent);

                    resultString += await result.Content.ReadAsStringAsync();
                }

                return(resultString);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(ex.Message);
            }
        }