/// <summary> /// Sends notifications to the specified device token and device platform . /// </summary> /// <param name="notification">An enum specifying the notification type.</param> public void SendNotification(Models.Notification notification) { int badgeCount = 0; ulong tempNumber; const string delimeter = ","; DevicePlatform devicePlatform; string deviceToken = null; switch (notification.NType) { case NotificationType.Im: #region IM notfication if (_notificationMsgLength == 0) { _notificationMsgLength = Convert.ToUInt16(ConfigurationManager.AppSettings[NotificationConstants.NotificationMsgLength]); } if (!NeeoUtility.IsNullOrEmpty(notification.DToken) && notification.Dp != null && notification.IMTone != null && !NeeoUtility.IsNullOrEmpty(notification.Alert) && !NeeoUtility.IsNullOrEmpty(notification.SenderID) && !NeeoUtility.IsNullOrEmpty(notification.ReceiverID) && ulong.TryParse(notification.ReceiverID, out tempNumber) && notification.Badge != 0) { NeeoUser receiver = new NeeoUser(notification.ReceiverID) { DeviceToken = notification.DToken.Trim(), ImTone = notification.IMTone.GetValueOrDefault(), OfflineMsgCount = notification.Badge, DevicePlatform = notification.Dp.GetValueOrDefault(), PnSource = notification.PnSource.HasValue ? notification.PnSource.Value : PushNotificationSource.Default }; if (notification.Alert.Length > _notificationMsgLength) { notification.Alert = notification.Alert.Substring(0, _notificationMsgLength) + "..."; } if (receiver.DevicePlatform == DevicePlatform.iOS) { _push.QueueNotification(new AppleNotification(receiver.DeviceToken) .WithAlert(notification.Alert) .WithBadge(receiver.OfflineMsgCount) .WithSound(receiver.ImTone.GetDescription()) .WithCustomItem(NotificationConstants.NotificationID, NotificationType.Im.ToString("D")) .WithCustomItem(NotificationConstants.SenderID, notification.SenderID)); } else if (receiver.DevicePlatform == DevicePlatform.Android) { Dictionary <string, string> payload = new Dictionary <string, string>() { { "alert", notification.Alert }, { NotificationConstants.NotificationID, NotificationType.Im.ToString("D") }, { NotificationConstants.SenderID, notification.SenderID } }; switch (receiver.PnSource) { case PushNotificationSource.Default: _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(receiver.DeviceToken) .WithJson(JsonConvert.SerializeObject(payload))); break; case PushNotificationSource.Pushy: var pushyRequest = new PushyPushRequest(payload, new string[] { receiver.DeviceToken }); PushyClient.SendPush(pushyRequest); break; } } else if (receiver.DevicePlatform == DevicePlatform.WindowsMobile) { var alertParts = notification.Alert.Split(new[] { ':' }, 2); string notificationMeta = string.Format("/NeeoPivot.xaml?PhoneNumber={0}&PushNotificationType={1}", notification.SenderID, NotificationType.Im.ToString("D")); var winToastNotification = new WindowsToastNotification() .WithLaunch(notificationMeta) .AsToastText02(alertParts[0].Trim(), alertParts[1].Trim()) .ForChannelUri(receiver.DeviceToken); var customAudio = new CustomToastAudio() { CustomSource = "ms-appx:///Assets/Sounds/beep.wav" }; winToastNotification.Audio = customAudio; _push.QueueNotification(winToastNotification); _push.QueueNotification(new WindowsBadgeNumericNotification().WithBadgeNumber(receiver.OfflineMsgCount).ForChannelUri(receiver.DeviceToken)); } else { //do nothing } } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } break; #endregion case NotificationType.Call: #region Incoming sip call notification if (_incomingCallingMsgText == null) { _incomingCallingMsgText = ConfigurationManager.AppSettings[NotificationConstants.IncomingCallingMsgText]; } if (!NeeoUtility.IsNullOrEmpty(notification.CallerID) && ulong.TryParse(notification.CallerID, out tempNumber) && !NeeoUtility.IsNullOrEmpty(notification.ReceiverID) && ulong.TryParse(notification.ReceiverID, out tempNumber)) { // Get the name of the user from data base. DbManager dbManager = new DbManager(); var userInfo = dbManager.GetUserInforForNotification(notification.ReceiverID, notification.CallerID, false); NeeoUser receiver = new NeeoUser(notification.ReceiverID) { CallingTone = (CallingTone)Convert.ToInt16(userInfo[NeeoConstants.ReceiverCallingTone]), DevicePlatform = (DevicePlatform)Convert.ToInt16(userInfo[NeeoConstants.ReceiverUserDeviceplatform]), }; if (!NeeoUtility.IsNullOrEmpty(userInfo[NeeoConstants.ReceiverDeviceToken]) && !NeeoUtility.IsNullOrEmpty(userInfo[NeeoConstants.CallerName])) { receiver.DeviceToken = userInfo[NeeoConstants.ReceiverDeviceToken].Trim(); receiver.PnSource = (PushNotificationSource)Convert.ToInt32(userInfo[NeeoConstants.PushNotificationSource]); } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } if (receiver.DevicePlatform == DevicePlatform.iOS) { if (_actionKeyText == null) { _actionKeyText = ConfigurationManager.AppSettings[NotificationConstants.ActionKeyText]; } if (_iosIncomingCallingTone == null) { _iosIncomingCallingTone = ConfigurationManager.AppSettings[NotificationConstants.IosIncomingCallingTone]; } _push.QueueNotification(new AppleNotification(receiver.DeviceToken) .WithAlert(_incomingCallingMsgText.Replace("[" + NeeoConstants.CallerName + "]", userInfo[NeeoConstants.CallerName]), "", _actionKeyText, new List <object>() { }) .WithSound(receiver.CallingTone.GetDescription()) .WithCustomItem(NotificationConstants.NotificationID, NotificationType.Call.ToString("D")) .WithCustomItem(NotificationConstants.Timestamp, DateTime.UtcNow.ToString(NeeoConstants.TimestampFormat))); } else if (receiver.DevicePlatform == DevicePlatform.Android) { Dictionary <string, string> payload = new Dictionary <string, string>() { { "alert", _incomingCallingMsgText.Replace("[" + NeeoConstants.CallerName + "]", userInfo[NeeoConstants.CallerName]) }, { NotificationConstants.NotificationID, NotificationType.Call.ToString("D") }, { NotificationConstants.Timestamp, DateTime.UtcNow.ToString(NeeoConstants.TimestampFormat) }, { NotificationConstants.CallerID, notification.CallerID } }; switch (receiver.PnSource) { case PushNotificationSource.Default: _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(receiver.DeviceToken) .WithJson(JsonConvert.SerializeObject(payload))); break; case PushNotificationSource.Pushy: var pushyRequest = new PushyPushRequest(payload, new string[] { receiver.DeviceToken }); PushyClient.SendPush(pushyRequest); break; } } else { // do nothing } } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } break; #endregion case NotificationType.Mcr: #region Mcr notification if (_mcrMsgText == null) { _mcrMsgText = ConfigurationManager.AppSettings[NotificationConstants.McrMsgText]; } if (!NeeoUtility.IsNullOrEmpty(notification.CallerID) && ulong.TryParse(notification.CallerID, out tempNumber) && !NeeoUtility.IsNullOrEmpty(notification.ReceiverID) && ulong.TryParse(notification.ReceiverID, out tempNumber) && notification.McrCount != 0) { DbManager dbManager = new DbManager(); var userInfo = dbManager.GetUserInforForNotification(notification.ReceiverID, notification.CallerID, true, notification.McrCount); NeeoUser receiver = new NeeoUser(notification.ReceiverID) { DevicePlatform = (DevicePlatform)Convert.ToInt16(userInfo[NeeoConstants.ReceiverUserDeviceplatform]), }; if (!NeeoUtility.IsNullOrEmpty(userInfo[NeeoConstants.ReceiverDeviceToken])) { receiver.DeviceToken = userInfo[NeeoConstants.ReceiverDeviceToken].Trim(); receiver.PnSource = (PushNotificationSource)Convert.ToInt32(userInfo[NeeoConstants.PushNotificationSource]); } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } badgeCount = Convert.ToInt32(userInfo[NeeoConstants.OfflineMessageCount]); if (receiver.DevicePlatform == DevicePlatform.iOS) { if (_iosApplicationMcrTone == null) { _iosApplicationMcrTone = ConfigurationManager.AppSettings[NotificationConstants.IosApplicationMcrTone]; } _push.QueueNotification(new AppleNotification(receiver.DeviceToken) .WithAlert(_mcrMsgText.Replace("[" + NeeoConstants.CallerName + "]", userInfo[NeeoConstants.CallerName])) .WithBadge(badgeCount) .WithSound(_iosApplicationMcrTone) .WithCustomItem(NotificationConstants.NotificationID, NotificationType.Mcr.ToString("D")) .WithCustomItem(NotificationConstants.CallerID, notification.CallerID)); } else if (receiver.DevicePlatform == DevicePlatform.Android) { Dictionary <string, string> payload = new Dictionary <string, string>() { { "alert", _mcrMsgText.Replace("[" + NeeoConstants.CallerName + "]", userInfo[NeeoConstants.CallerName]) }, { NotificationConstants.NotificationID, NotificationType.Mcr.ToString("D") }, { NotificationConstants.CallerID, notification.CallerID } }; switch (receiver.PnSource) { case PushNotificationSource.Default: _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(receiver.DeviceToken) .WithJson(JsonConvert.SerializeObject(payload))); break; case PushNotificationSource.Pushy: var pushyRequest = new PushyPushRequest(payload, new string[] { receiver.DeviceToken }); PushyClient.SendPush(pushyRequest); break; } } else { //do nothing } } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } break; #endregion case NotificationType.GroupIm: #region Group notification if (!NeeoUtility.IsNullOrEmpty(notification.SenderID) && notification.RID != 0 && !NeeoUtility.IsNullOrEmpty(notification.RName) && !NeeoUtility.IsNullOrEmpty(notification.Alert) && notification.MessageType != null) { List <NeeoUser> lstgroupParticipant = NeeoGroup.GetGroupParticipants(notification.RID, notification.SenderID, (int)notification.MessageType); if (lstgroupParticipant.Count > 0) { var taskUpdateOfflineCount = Task.Factory.StartNew(() => { LogManager.CurrentInstance.InfoLogger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Group participants - " + JsonConvert.SerializeObject(lstgroupParticipant), System.Reflection.MethodBase.GetCurrentMethod().Name); IEnumerable <string> userList = from item in lstgroupParticipant where item.PresenceStatus == Presence.Offline select item.UserID; string userString = string.Join(delimeter, userList); //lstgroupParticipant.Where(i => i.PresenceStatus == Presence.Offline).Select(a => a.UserID) // .Aggregate((current, next) => current + delimeter + next); LogManager.CurrentInstance.InfoLogger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "User list - " + userString, System.Reflection.MethodBase.GetCurrentMethod().Name); if (!NeeoUtility.IsNullOrEmpty(userString)) { DbManager dbManager = new DbManager(); dbManager.UpdateUserOfflineCount(userString, delimeter); } }); var taskScheduleNotifications = Task.Factory.StartNew(() => { Parallel.ForEach(lstgroupParticipant, item => //foreach (var item in lstgroupParticipant) { if (!NeeoUtility.IsNullOrEmpty(item.DeviceToken) && item.DeviceToken != "-1" && item.PresenceStatus == Presence.Offline) { if (item.DevicePlatform == DevicePlatform.iOS) { _push.QueueNotification(new AppleNotification( item.DeviceToken.Trim()) .WithAlert(notification.Alert) .WithBadge(item.OfflineMsgCount + 1) .WithSound(item.ImTone.GetDescription()) .WithCustomItem(NotificationConstants.NotificationID, NotificationType.GroupIm.ToString("D")) .WithCustomItem(NotificationConstants.RoomID, notification.RName)); } else if (item.DevicePlatform == DevicePlatform.Android) { Dictionary <string, string> payload = new Dictionary <string, string>() { { "alert", notification.Alert }, { NotificationConstants.NotificationID, NotificationType.GroupIm.ToString("D") }, { NotificationConstants.RoomID, notification.RName } }; switch (item.PnSource) { case PushNotificationSource.Default: _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(item.DeviceToken) .WithJson(JsonConvert.SerializeObject(payload))); break; case PushNotificationSource.Pushy: var pushyRequest = new PushyPushRequest(payload, new string[] { item.DeviceToken }); PushyClient.SendPush(pushyRequest); break; } } else { //do nothing } } }); }); taskUpdateOfflineCount.Wait(); taskScheduleNotifications.Wait(); } } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } break; #endregion case NotificationType.GroupInvite: #region Group invitation notification if (!NeeoUtility.IsNullOrEmpty(notification.DToken) && notification.Dp != null && notification.IMTone != null && !NeeoUtility.IsNullOrEmpty(notification.Alert) && !NeeoUtility.IsNullOrEmpty(notification.RName) && !NeeoUtility.IsNullOrEmpty(notification.ReceiverID) && ulong.TryParse(notification.ReceiverID, out tempNumber)) { NeeoUser receiver = new NeeoUser(notification.ReceiverID) { DeviceToken = notification.DToken.Trim(), ImTone = notification.IMTone.GetValueOrDefault(), OfflineMsgCount = notification.Badge, DevicePlatform = notification.Dp.GetValueOrDefault(), PnSource = notification.PnSource.HasValue ? notification.PnSource.Value : PushNotificationSource.Default }; if (receiver.DevicePlatform == DevicePlatform.iOS) { _push.QueueNotification(new AppleNotification(receiver.DeviceToken) .WithAlert(notification.Alert) .WithBadge(receiver.OfflineMsgCount) .WithSound(receiver.ImTone.GetDescription()) .WithCustomItem(NotificationConstants.NotificationID, NotificationType.GroupIm.ToString("D")) .WithCustomItem(NotificationConstants.RoomID, notification.RName)); } else if (receiver.DevicePlatform == DevicePlatform.Android) { Dictionary <string, string> payload = new Dictionary <string, string>() { { "alert", notification.Alert }, { NotificationConstants.NotificationID, NotificationType.GroupIm.ToString("D") }, { NotificationConstants.RoomID, notification.RName } }; switch (receiver.PnSource) { case PushNotificationSource.Default: _push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(receiver.DeviceToken) .WithJson(JsonConvert.SerializeObject(payload))); break; case PushNotificationSource.Pushy: var pushyRequest = new PushyPushRequest(payload, new string[] { receiver.DeviceToken }); PushyClient.SendPush(pushyRequest); break; } } else { //do nothing } } else { throw new ApplicationException(CustomHttpStatusCode.InvalidArguments.ToString("D")); } break; #endregion } }
/// <summary> /// The main processor method used to process a single push notification, checks if the processing will be an immediate single push or regular thread looping model. /// Looks up for a single (or more if you wish) entity in the databae which has not been processed. /// Puts the fetched unprocessed push notification entity to processing over the Push Sharp API. /// Finally saves the state of processing. /// </summary> /// <param name="databaseContext">The current database context to be used for processing to the database.</param> /// <param name="pushNotification">A single push notification entity to be processed and saved.</param> /// <param name="isDirectSinglePush">Decides wethere the processing will take place immediately for the sent notification or will the method lookup from the database for a first unprocessed push notification.</param> /// <returns>True if all OK, false if not.</returns> public bool ProcessNotification(PushSharpDatabaseContext dbContext, PushNotification pushNotification = null, bool isDirectSinglePush = false) { _databaseContext = dbContext; _isDirectSinglePush = isDirectSinglePush; if (_isDirectSinglePush) { InitBroker(); } On(DisplayMessage, "Checking for unprocessed notifications..."); PushNotification notificationEntity = pushNotification; try { if (notificationEntity != null) { // save a new immediate unprocessed push notification _databaseContext.PushNotification.Add(pushNotification); _databaseContext.SaveChanges(); // reload the entity notificationEntity = _databaseContext.PushNotification .Where(x => x.ID == pushNotification.ID) .Include(x => x.MobileDevice) .Include(x => x.MobileDevice.Client) .FirstOrDefault(); } else // take one latest unprocessed notification, this can be changed to take any set size instead of one { notificationEntity = _databaseContext.PushNotification.FirstOrDefault(s => s.Status == (int)PushNotificationStatus.Unprocessed && s.CreatedAt <= DateTime.Now); } } catch (Exception ex) { On(DisplayErrorMessage, "EX. ERROR: Check for unprocessed notifications: " + ex.Message); SimpleErrorLogger.LogError(ex); } // Process i.e. push the push notification via PushSharp... if (notificationEntity != null) { bool messagePushed = true; On(DisplayStatusMessage, "Processing notification..."); On(DisplayMessage, "ID " + notificationEntity.ID + " for " + notificationEntity.MobileDevice.Client.Username + " -> " + notificationEntity.Message); //--------------------------- // ANDROID GCM NOTIFICATIONS //--------------------------- if (notificationEntity.MobileDevice.SmartphonePlatform == "android") { var gcmNotif = new GcmNotification() { Tag = notificationEntity.ID }; string msg = JsonConvert.SerializeObject(new { message = notificationEntity.Message }); gcmNotif.ForDeviceRegistrationId(notificationEntity.MobileDevice.PushNotificationsRegistrationID) .WithJson(msg); _broker.QueueNotification(gcmNotif); UpdateNotificationQueued(notificationEntity); } ////------------------------- //// APPLE iOS NOTIFICATIONS ////------------------------- else if (notificationEntity.MobileDevice.SmartphonePlatform == "ios") { var appleNotif = new AppleNotification() { Tag = notificationEntity.ID }; var msg = new AppleNotificationPayload(notificationEntity.Message); appleNotif.ForDeviceToken(notificationEntity.MobileDevice.PushNotificationsRegistrationID) .WithPayload(msg) .WithSound("default"); _broker.QueueNotification(appleNotif); UpdateNotificationQueued(notificationEntity); } //---------------------- // WINDOWS NOTIFICATIONS //---------------------- else if (notificationEntity.MobileDevice.SmartphonePlatform.Equals("wp") || notificationEntity.MobileDevice.SmartphonePlatform.Equals("wsa")) { var wNotif = new WindowsToastNotification() { Tag = notificationEntity.ID }; wNotif.ForChannelUri(notificationEntity.MobileDevice.PushNotificationsRegistrationID) .AsToastText02("PushSharp Notification", notificationEntity.Message); _broker.QueueNotification(wNotif); UpdateNotificationQueued(notificationEntity); } else { On(DisplayErrorMessage, "ERROR: Unsupported device OS: " + notificationEntity.MobileDevice.SmartphonePlatform); notificationEntity.Status = (int)PushNotificationStatus.Error; notificationEntity.ModifiedAt = DateTime.Now; notificationEntity.Description = "(Processor) Unsupported device OS: " + notificationEntity.MobileDevice.SmartphonePlatform; SimpleErrorLogger.LogError(new Exception("EX. ERROR: " + notificationEntity.Description)); messagePushed = false; } try { // Save changes to DB to keep the correct state of messages _databaseContext.SaveChanges(); // bubble out the single push error, else return true to continue iteration if (_isDirectSinglePush) { return(messagePushed); } return(true); } catch (Exception ex) { On(DisplayErrorMessage, "EX. ERROR: Updating notification, DB save failed: " + ex.Message); SimpleErrorLogger.LogError(ex); // bubble out the single push error, else return true to continue iteration if (_isDirectSinglePush) { return(false); } return(true); } finally { if (_isDirectSinglePush) { KillBroker(_databaseContext); } } } else { if (_isDirectSinglePush) { KillBroker(_databaseContext); } // no messages were queued, take a nap... return(false); } }