public async Task UnregisterDeviceToken(int userId, string deviceToken) { log.InfoFormat("UnregisterDeviceToken: userId={0}, deviceToken={1}", userId, deviceToken); AmznSnsWrapper snsWrapper = new AmznSnsWrapper(); var device = db.UsersDvices.Where(t => t.UserId == userId && t.DeviceToken == deviceToken).FirstOrDefault(); if (device != null) { db.UsersDvices.Remove(device); if (device.ServiceToken != null) { await snsWrapper.RemoveEndpoint(device.EndpointArn); } db.SaveChanges(); } else { log.Info("UnregisterDeviceToken: Device Token doesn't exists"); } }
public async Task SaveUserDeviceToken(int userId, string deviceToken, int servToken, bool isIOS, string section) { log.DebugFormat("SaveUserDeviceToken: userId={0}, deviceToken={1}, serveToken={2}, section={3}", userId, deviceToken, servToken, section); var sectionObj = db.Sections.Where(s => s.Alias == section) .FirstOrDefault(); var sectionId = (sectionObj != null ? sectionObj.SectionId : 0); var device = db.UsersDvices.FirstOrDefault(t => t.UserId == userId && t.DeviceToken == deviceToken && t.ServiceToken == servToken && t.SectionId == sectionId); if (device == null) { device = new UsersDvice { UserId = userId, DeviceToken = deviceToken, ServiceToken = servToken, DeviceType = isIOS ? UserDeviceType.IOS : UserDeviceType.Android, ServiceType = isIOS ? NotificationServiceType.APNS : NotificationServiceType.GCM, SectionId = (sectionObj != null ? sectionObj.SectionId : 0) }; db.UsersDvices.Add(device); log.Info("SaveUserDeviceToken: Device Token added"); } else { log.Info("SaveUserDeviceToken: Device Token already exists"); } if (deviceToken != null && device.EndpointArn == null) { try { AmznSnsWrapper snsWrapper = new AmznSnsWrapper(); AmznSnsWrapper.Platform mobAppPlatform = isIOS ? AmznSnsWrapper.Platform.APNS : AmznSnsWrapper.Platform.GCM; AmznSnsWrapper.MobileAppForSports mobAppForSports = 0; if (sectionObj != null) { if ("basketball".Equals(sectionObj.Alias)) { mobAppForSports = AmznSnsWrapper.MobileAppForSports.Basketball; } else if ("volleyball".Equals(sectionObj.Alias)) { mobAppForSports = AmznSnsWrapper.MobileAppForSports.Volleyball; } else if ("netball".Equals(sectionObj.Alias)) { mobAppForSports = AmznSnsWrapper.MobileAppForSports.Netball; } else if ("waterpolo".Equals(sectionObj.Alias)) { mobAppForSports = AmznSnsWrapper.MobileAppForSports.Waterpolo; } } device.EndpointArn = await snsWrapper.CreateEndpoint(deviceToken, mobAppPlatform, mobAppForSports); } catch (Exception e) { log.ErrorFormat("SaveUserDeviceToken: Error while registering ARN from Amazon for User Device Token: {0}", e.ToString()); } } db.SaveChanges(); }
public async Task SendPushToDevices(bool isTest) { log.Info("SendPushToDevices"); var recList = (from m in db.NotesMessages from r in m.NotesRecipients from u in r.User.UsersDvices where ((m.TypeId & MessageTypeEnum.NoPushNotify) == 0) && r.IsPushSent == false && u.ServiceType != null && u.EndpointArn != null select new { m.MsgId, m.Message, r.UserId, u.ServiceToken, u.ServiceType, u.EndpointArn }).ToList(); if (recList.Count > 0) { var snsWrapper = new AmznSnsWrapper(); foreach (var msg in recList.GroupBy(g => new { g.MsgId, g.Message, g.ServiceType })) { var endpointsArnArr = msg.Select(t => new { t.EndpointArn, t.UserId }).ToArray(); string rawMsg = null; try { log.InfoFormat("SendPushToDevices: Sending. MsgId={0}", msg.Key.MsgId); switch (msg.Key.ServiceType) { case NotificationServiceType.APNS: rawMsg = snsWrapper.BuildPlatformMessage(AmznSnsWrapper.Platform.APNS, msg.Key.Message).ToString(); break; case NotificationServiceType.GCM: rawMsg = snsWrapper.BuildPlatformMessage(AmznSnsWrapper.Platform.GCM, msg.Key.Message).ToString(); break; } foreach (var ea in endpointsArnArr) { switch (snsWrapper.SendMessage(ea.EndpointArn.Trim(), rawMsg)) { case 0: log.InfoFormat("SendPushToDevices: Success. UserId={0}, EndpointArn={1}", ea.UserId, ea.EndpointArn); // success: break; case -1: // EndpointArn Disabled UsersDvice ud = db.UsersDvices.First(d => d.EndpointArn == ea.EndpointArn); if (ud != null) { db.UsersDvices.Remove(ud); db.SaveChanges(); } log.InfoFormat("SendPushToDevices: EndpointArd Disabled. Removed Device. UserId={0}, EndpointArn={1}", ea.UserId, ea.EndpointArn); break; case -9999: log.InfoFormat("SendPushToDevices: Unknown Error. UserId={0}, EndpointArn={1}", ea.UserId, ea.EndpointArn); // other exception occured break; } } } catch (Exception e) { log.Debug("Error while trying to send message to device", e); } } foreach (var item in db.NotesRecipients.Where(t => t.IsPushSent == false)) { item.IsPushSent = true; } db.SaveChanges(); } else { log.Info("SendPushToDevices: Nothing to send"); } }