private async Task SendPushNotificationAsync(Post post) { string senderName = null; string senderImageUrl = null; using (var db = new timelineformsContext()) { var sender = db.Users.Find(post.SenderId); senderName = sender.FullName; senderImageUrl = sender.ImageUrl; } // Sending the message so that all template registrations that contain the following params // will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations. var templateParams = new Dictionary <string, string> { ["action"] = $"post:{post.Id}", ["title"] = $"New post from {senderName}", ["message"] = post.Text, ["image"] = senderImageUrl }; try { // Sends the push notification. var receivers = $"!{User.GetNotificationTag()}"; var result = await hubClient.SendTemplateNotificationAsync(templateParams, receivers); } catch { } }
protected override void Initialize(HttpControllerContext controllerContext) { base.Initialize(controllerContext); timelineformsContext context = new timelineformsContext(); DomainManager = new EntityDomainManager <Post>(context, Request); hubClient = this.GetHubNotificationClient(); }
public async Task <IHttpActionResult> GetTimeline() { using (var db = new timelineformsContext()) { var posts = db.Posts.OrderByDescending(p => p.SentDate).Take(50) .Select(p => new TimelinePost { PostId = p.Id, SentDate = p.SentDate, Text = p.Text, SenderId = p.SenderId, Sender = new User { UserId = p.Sender.Id, EMail = p.Sender.EMail, FirstName = p.Sender.FirstName, LastName = p.Sender.LastName, ImageUrl = p.Sender.ImageUrl, ProfileUrl = p.Sender.ProfileUrl }, Comments = p.Comments.OrderByDescending(c => c.SentDate).Take(1) .Select(c => new Comment { CommentId = c.Id, SentDate = c.SentDate, Text = c.Text, Sender = new User { UserId = c.Sender.Id, EMail = c.Sender.EMail, FirstName = c.Sender.FirstName, LastName = c.Sender.LastName, ImageUrl = c.Sender.ImageUrl, ProfileUrl = c.Sender.ProfileUrl }, }).ToList(), TotalCommentsCount = p.Comments.Count, PublishOnFacebook = p.PublishOnFacebook }); var result = await posts.ToListAsync(); return(Ok(result)); } }
private async Task SendPushNotificationAsync(Comment comment) { string senderName = null; string senderImageUrl = null; string postAuthorId = null; using (var db = new timelineformsContext()) { postAuthorId = db.Posts.Find(comment.PostId).SenderId; // If the author of the comment is the same of the post, we don't need // to send notification. if (postAuthorId == comment.SenderId) { return; } var sender = db.Users.Find(comment.SenderId); senderName = sender.FullName; senderImageUrl = sender.ImageUrl; } // Sending the message so that all template registrations that contain the following params // will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations. var templateParams = new Dictionary <string, string> { ["action"] = $"comment:{comment.Id}|post:{comment.PostId}", ["title"] = $"{senderName} commented your post", ["message"] = comment.Text, ["image"] = senderImageUrl }; try { // Sends the push notification. var receiver = NotificationHubExtensions.GetNotificationTagFor(postAuthorId); var result = await hubClient.SendTemplateNotificationAsync(templateParams, receiver); } catch { } }
public async Task <IHttpActionResult> GetMe() { //var settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); //var traceWriter = this.Configuration.Services.GetTraceWriter(); var userId = this.User.GetUserId(); var creds = await this.User.GetAppServiceIdentityAsync <FacebookCredentials>(this.Request); string facebookUserId = null; string firstName = null; string lastName = null; string eMail = null; string imageUrl = null; string profileUrl = null; using (var db = new timelineformsContext()) { var userDb = db.Users.FirstOrDefault(u => u.Id == userId); if (userDb == null) { // Inserts the user into the database. facebookUserId = creds.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; firstName = creds.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName).Value; lastName = creds.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.Surname).Value; eMail = creds.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value; profileUrl = creds.UserClaims.FirstOrDefault(c => c.Type == FacebookProfileUrlClaim).Value; imageUrl = $"https://graph.facebook.com/{facebookUserId}/picture?width=150"; userDb = new DataObjects.User { Id = userId, FacebookUserId = facebookUserId, FirstName = firstName, LastName = lastName, EMail = eMail, ImageUrl = imageUrl, ProfileUrl = profileUrl, Deleted = false }; db.Users.Add(userDb); await db.SaveChangesAsync(); } else { // The user already exists. Retrieves the information. facebookUserId = userDb.FacebookUserId; firstName = userDb.FirstName; lastName = userDb.LastName; eMail = userDb.EMail; imageUrl = userDb.ImageUrl; profileUrl = userDb.ProfileUrl; } } var user = new User { UserId = userId, FacebookUserId = facebookUserId, FirstName = firstName, LastName = lastName, EMail = eMail, ImageUrl = imageUrl, ProfileUrl = profileUrl }; return(Ok(user)); }