private static byte[] GetGcmData(string userNameFromComment, IEnumerable<User> users, string message, Feeling feeling) { var jsonObject = new JObject(); var arr = new JArray(); users.Where(u => !string.IsNullOrWhiteSpace(u.Key)).ForEach(u => arr.Add(u.Key)); if (!arr.Any()) return null; jsonObject.Add("registration_ids", arr); var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var data = new JObject { {"message", message}, {"user", userNameFromComment}, {"feeling", JsonConvert.SerializeObject(feeling.Id, Formatting.Indented, jsonSerializerSettings)} }; jsonObject.Add("data", data); var postData = JsonConvert.SerializeObject(jsonObject); var byteArray = Encoding.UTF8.GetBytes(postData); return byteArray; }
private void SendNotification(Feeling feeling, Comment comment, List<User> users, User feelingUser) { try { new PushNotificationService().SendCommentNotifications(feeling, comment, users, SaveResponse, feelingUser); } catch (Exception e) { LogWriter.Write(e.ToString()); } }
public void SendCommentNotifications(Feeling feeling, Comment comment, List<User> users, Action<string> action, User feelingUser) { _action = action; SendNotifications(comment.User, new List<User> { feelingUser }, string.Format("Comment '{0}' on feeling: '{1}' from ", comment.Text, feeling.FeelingText), feeling); SendNotifications(comment.User, users, string.Format("Comment '{0}' on comment", comment.Text), feeling); }
private void SendNotifications(string userNameFromComment, List<User> users, string message, Feeling feeling) { // Create a request using a URL that can receive a post. if (users.Count() == 1 && users.First().UserName.Equals(userNameFromComment) || !users.Any()) return; var byteArray = GetGcmData(userNameFromComment, users, message, feeling); if (byteArray != null) SendAndroidNotification(byteArray); SendIosNotification(users.Where(u => !string.IsNullOrWhiteSpace(u.IosKey)).Select(u => u.IosKey), string.Format("{0} from {1}", message, userNameFromComment), new Dictionary<string, string> { { "feelingId", feeling.Id } }); }
private dynamic SendNotification(Feeling feeling) { var feelings = Context.Feelings.Find(Query.And(Query.EQ("FeelingText", new BsonString(feeling.FeelingText)), Query.EQ("IsCurrentFeeling", new BsonBoolean(true)))); var users = new List<User>(); foreach (var f in feelings) { var user = Context.Users.FindOne(Query.EQ("UserName", new BsonString(f.UserName))); if (user == null || (string.IsNullOrWhiteSpace(user.Key) && string.IsNullOrWhiteSpace(user.IosKey)) || user.UserName.Equals(feeling.UserName)) continue; users.Add(user); } if (!users.Any()) LogWriter.Write("No users found for same feeling"); users.ForEach(u => LogWriter.Write(string.Format("{0} Key:{1} IosKey:{2}\n", u.UserName, u.Key, u.IosKey))); var gcmUserKeys = users.Where(u => !string.IsNullOrWhiteSpace(u.Key)).Select(x => x.Key); var iosUserKeys = users.Where(u => !string.IsNullOrWhiteSpace(u.IosKey)).Select(x => x.IosKey); new PushNotificationService().SendSameFeelingNotification(gcmUserKeys, iosUserKeys, string.Format("User {0} has just added a feeling as {1}. Start sharing.", feeling.UserName, feeling.FeelingText)); return true; }