public bool CreateSnsEndPointForAccount(Account account, string snsToken, MobilePlatformType mobilePlatform, out SNSEndpoint snsEndpoint, DateTime?lastLoginDate = null, bool isLoggedIn = true, bool isActive = true) { snsEndpoint = new SNSEndpoint { Account = account, CreateDate = DateTime.Now, IsActive = isActive, IsLoggedIn = isLoggedIn, LastLoginDate = lastLoginDate, MobilePlatformType = mobilePlatform, SNSToken = snsToken, SnsEndpointAuthKey = account.CobrandToUse.CobrandSettings.CobrandAuthKey }; try { DB.SNSEndpoints.Add(snsEndpoint); DB.SaveChanges(); return(true); } catch (Exception ex) { ExceptionHelper.Log(ex); return(false); } }
private void SendNotification(string jsonObj, SNSEndpoint endpoint, int retries) { if (retries == 0) { return; } //calculate the snsauthkey var snsAuthKeyToUse = endpoint.SnsEndpointAuthKey; if (string.IsNullOrEmpty(snsAuthKeyToUse)) { snsAuthKeyToUse = "AIzaSyDPr3u0jzTYoNzmrauUprXbvd9su4D4lgk"; } try { var tRequest = WebRequest.Create(ConfigHelper.TryGetOrDefault("FCMWebRequestUri", "https://fcm.googleapis.com/fcm/send")); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var byteArray = Encoding.UTF8.GetBytes(jsonObj); //tRequest.Headers.Add(string.Format("Authorization: key={0}", ConfigHelper.TryGetOrDefault("FCMWebRequestAuthorizationToken", "AIzaSyDPr3u0jzTYoNzmrauUprXbvd9su4D4lgk"))); tRequest.Headers.Add(string.Format("Authorization: key={0}", ConfigHelper.TryGetOrDefault("FCMWebRequestAuthorizationToken", snsAuthKeyToUse))); tRequest.ContentLength = byteArray.Length; using (var dataStream = tRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); try { using (var tResponse = tRequest.GetResponse()) { using (var dataStreamResponse = tResponse.GetResponseStream()) { using (var tReader = new StreamReader(dataStreamResponse)) { var responseFromServer = tReader.ReadToEnd(); var response = JsonConvert.DeserializeObject <FCMResponse>(responseFromServer); if (response.success == 1) { LogHelper.LogInfo( string.Format("SNS message Id: {0}. Success.", response.results[0].message_id), "SNS.SendNotification"); } else if (response.failure == 1) { LogHelper.LogError( string.Format("SNS token: {0}. Failure.", endpoint), "SNS.SendNotification"); } } } } } catch (Exception ex) { retries--; LogHelper.LogError( string.Format("SNS token: {0}. Failure. {1} Attempt Left. Message: {2}", endpoint, retries, ex.Message), "SNS.SendNotificationAsync"); Thread.Sleep(1000); SendNotification(jsonObj, endpoint, retries); } } } catch (Exception ex) { ExceptionHelper.Log(ex, "SNS.SendNotificationAsync"); } }