Example #1
0
 public static void SetCloudConfigDataModel(CloudConfigurationSettingsModel configSetting)
 {
     if (configSetting != null)
     {
         cloudConfiguration = configSetting;
     }
 }
        public static void SendNotification(string title, string body)
        {
            CloudConfigurationSettingsModel cloudConfig = ConfigurationSettings.GetCloudConfigDataModel();

            if (!((string.IsNullOrEmpty(cloudConfig.ApplicationId)) || (string.IsNullOrEmpty(cloudConfig.NotificationSender)) || (string.IsNullOrEmpty(cloudConfig.NotificationReceiver)) ||
                  (string.IsNullOrEmpty(cloudConfig.ClickAction)) || (string.IsNullOrEmpty(cloudConfig.FCMURL)) || (string.IsNullOrEmpty(cloudConfig.Icon))))
            {
                try
                {
                    //var applicationID = "AAAAGQBSH1c:APA91bEcYFZQMez7DyNTgphhxk1Sw4uKgss0xW7qBqiMX9QBHPNeIItIrw8VhvCJVWi8WUGUMPdRrx64P82lUtzmPUdvKFKYdr_UJHQl6lnWrXeK0J6-QHZaqkhsAKw1J3TwUievGRA2";
                    var applicationID = cloudConfig.ApplicationId;

                    //var senderId = "107379564375";
                    var senderId = cloudConfig.NotificationSender;

                    //string receiver = "/topics/Alerts";
                    string receiver = cloudConfig.NotificationReceiver;

                    //WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                    WebRequest tRequest = WebRequest.Create(cloudConfig.FCMURL);
                    tRequest.Method      = "post";
                    tRequest.ContentType = "application/json";
                    var data = new
                    {
                        //data = new
                        //{
                        notification = new
                        {
                            body         = body,
                            title        = title,
                            click_action = cloudConfig.ClickAction,
                            icon         = cloudConfig.Icon,
                            sound        = "default"
                                           //  }
                        },
                        to = receiver
                    };

                    var json = JsonConvert.SerializeObject(data);

                    //var json = "{ \"notification\": {\"title\": \"Notification from csu\",\"text\": \"Notification from csu\",\"click_action\": \"http://localhost:65159/#/login \"},\"to\" : \"/topics/Alerts\"}";

                    Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
                    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                    tRequest.ContentLength = byteArray.Length;

                    using (Stream dataStream = tRequest.GetRequestStream())
                    {
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        using (WebResponse tResponse = tRequest.GetResponse())
                        {
                            using (Stream dataStreamResponse = tResponse.GetResponseStream())
                            {
                                using (StreamReader tReader = new StreamReader(dataStreamResponse))
                                {
                                    String sResponseFromServer = tReader.ReadToEnd();
                                    string str = sResponseFromServer;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string str = ex.Message;
                }
            }
            else
            {
                Console.WriteLine("Not having sufficient value to send notification");
            }
        }
        private CloudConfigurationSettingsModel GeCloudConfigurationSettings(string piServerName)
        {
            CloudConfigurationSettingsModel settingModel = new CloudConfigurationSettingsModel();

            try
            {
                SqlConnection getConfigurationConnection = new SqlConnection(ConfigurationSettings.AzureConnectionString);
                getConfigurationConnection.Open();
                //SqlCommand sqlCommandConfigurationList = new SqlCommand("SELECT ApplicationId,SenderId,Receiver,FCMURL,ClickAction,Icon FROM Configuration", getConfigurationListConnection);
                SqlCommand sqlCommandConfiguration = new SqlCommand("Select ConfigurationKey,ConfigurationValue from ApplicationConfigurationEntry where ApplicationConfigurationId = (select id from applicationconfiguration where ConfigurationType = @Firebase)", getConfigurationConnection);
                sqlCommandConfiguration.Parameters.Add(new SqlParameter("@Firebase", Constants.CLOUD_CONFIGURATION_TYPE_FIREBASE));
                SqlDataReader configurationsqlDataReader = sqlCommandConfiguration.ExecuteReader();

                while (configurationsqlDataReader.Read())
                {
                    switch (configurationsqlDataReader["ConfigurationKey"].ToString())
                    {
                    case Constants.CLOUD_CONFIGURATION_NOTIFICATIONAUTHORIZATION_Key:

                    { settingModel.ApplicationId = configurationsqlDataReader["ConfigurationValue"].ToString(); }
                    break;

                    case Constants.CLOUD_CONFIGURATION_FIREBASE_API_KEY:

                    { settingModel.ApiKey = configurationsqlDataReader["ConfigurationValue"].ToString(); }
                    break;

                    case Constants.CLOUD_CONFIGURATION_NOTIFICATION_SENDER_KEY:

                    { settingModel.NotificationSender = configurationsqlDataReader["ConfigurationValue"].ToString(); }
                    break;

                    case Constants.CLOUD_CONFIGURATION_NOTIFICATION_RECEIVER_KEY:

                    { settingModel.NotificationReceiver = configurationsqlDataReader["ConfigurationValue"].ToString(); }
                    break;

                    case Constants.CLOUD_CONFIGURATION_NOTIFICATION_CLICK_ACTION_KEY:

                    { settingModel.ClickAction = configurationsqlDataReader["ConfigurationValue"].ToString(); }
                    break;
                    }
                }
                settingModel.FCMURL = Constants.CLOUD_CONFIGURATION_FIREBASE_SEND_URL_VALUE;
                settingModel.Icon   = Constants.CLOUD_CONFIGURATION_FIREBASE_ICON_VALUE;
                configurationsqlDataReader.Close();

                //for blob storage configuration
                sqlCommandConfiguration = new SqlCommand("Select ConfigurationKey,ConfigurationValue from ApplicationConfigurationEntry where ApplicationConfigurationId = (select id from applicationconfiguration where ConfigurationType= @BlobStorage )", getConfigurationConnection);
                sqlCommandConfiguration.Parameters.Add(new SqlParameter("@BlobStorage", Constants.CLOUD_CONFIGURATION_TYPE_BLOB_STORAGE));

                configurationsqlDataReader = sqlCommandConfiguration.ExecuteReader();
                while (configurationsqlDataReader.Read())
                {
                    if (configurationsqlDataReader["ConfigurationKey"].ToString() == Constants.CLOUD_CONFIGURATION_STORAGE_CONNECTION_STRING_KEY)
                    {
                        settingModel.BlobStorageURL = configurationsqlDataReader["ConfigurationValue"].ToString();
                    }
                }

                ConnectionManager.Instance().CloseSQLConnection(getConfigurationConnection);
                return(settingModel);
            }
            catch (Exception ex)
            {
                Utility.Log("Exception occured in get cloud Configuration settings " + ex.Message);
                return(settingModel);
            }
        }