public async Task <IActionResult> SystemNotificationAsync([FromBody] SystemNotificationView view)
        {
            var result = await _service.SystemNotificationAsync(view);

            return(Ok(result));
        }
示例#2
0
        public async Task <bool> SendSystemPushNotificationAsync(SystemNotificationView content, string deviceId)
        {
            string response;

            try
            {
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");

                tRequest.Method      = "post";
                tRequest.ContentType = "application/json";

                var data = new PushNotificationModel()
                {
                    to           = deviceId,
                    data         = content,
                    notification = new FcmNotification()
                    {
                        body  = content.Message,
                        title = "System Notification",
                        sound = "Enabled"
                    }
                };
                _logger.Info($"Notification {data.ToJsonString()}");


                //var serializer = new JavaScriptSerializer();
                var json      = JsonConvert.SerializeObject(data);
                var byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add($"Authorization: key={_appSettings.ServerKey}");
                tRequest.Headers.Add($"Sender: id={_appSettings.SenderId}");
                tRequest.ContentLength = byteArray.Length;

                using (Stream dataStream = await tRequest.GetRequestStreamAsync())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    using (WebResponse tResponse = await tRequest.GetResponseAsync())
                    {
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        {
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                string sResponseFromServer = await tReader.ReadToEndAsync();

                                response = sResponseFromServer;
                                var responseData = JsonConvert.DeserializeObject <PushNotifyResponse>(response);

                                if (responseData.results[0].error == "NotRegistered" && responseData.success == 0)
                                {
                                    var dbConnectionString = _dbSettings.sqlConnection;
                                    using (var context = new AppDbContext(dbConnectionString))
                                    {
                                        var userDevice = await context.UserDeviceInfos.FirstOrDefaultAsync(k => k.DeviceId == deviceId);

                                        if (userDevice != null)
                                        {
                                            context.UserDeviceInfos.Remove(userDevice);
                                            await context.SaveChangesAsync();
                                        }
                                    }
                                }

                                _logger.Info(response);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;

                _logger.Error(ex);
                return(false);
            }

            return(true);
        }