public void Save(int deviceID, PushNotification notification)
        {
            var insertQuery = new SqlInsert("push_notification")
                .InColumnValue("device_id", deviceID)
                .InColumnValue("module", notification.Module)
                .InColumnValue("action", notification.Action)
                .InColumnValue("item_type", notification.Item.Type)
                .InColumnValue("item_id", notification.Item.ID)
                .InColumnValue("item_description", notification.Item.Description)
                .InColumnValue("queued_on", DateTime.UtcNow);

            if (notification.ParentItem != null)
                insertQuery
                    .InColumnValue("parent_type", notification.ParentItem.Type)
                    .InColumnValue("parent_id", notification.ParentItem.ID)
                    .InColumnValue("parent_description", notification.ParentItem.Description);

            using (var db = GetDbManager())
            {
                db.ExecuteNonQuery(insertQuery);
            }
        }
 public void EnqueueNotification(int tenantID, string userID, PushNotification notification, List <string> targetDevices)
 {
     Channel.EnqueueNotification(tenantID, userID, notification, targetDevices);
 }
Example #3
0
            public GetFeedResponse(PushNotification notification)
            {
                Module = notification.Module.ToString().ToLowerInvariant();
                Action = notification.Action.ToString().ToLowerInvariant();
                ItemType = notification.Item.Type.ToString().ToLowerInvariant();
                ItemID = notification.Item.ID;
                ItemDescription = notification.Item.Description;
                QueuedOn = (ApiDateTime)notification.QueuedOn;

                if (notification.ParentItem != null)
                    AdditionalInfo = new Dictionary<string, string>
                        {
                            {"parent_type", notification.ParentItem.Type.ToString().ToLowerInvariant()},
                            {"parent_id", notification.ParentItem.ID},
                            {"parent_title", notification.ParentItem.Description}
                        };
            }
        public List<PushNotification> GetNotifications(int tenantID, string userID, string deviceToken, DateTime from, DateTime to)
        {
            var selectQuery = new SqlQuery("push_notification t1")
                .Select("t1.module", "t1.action", "t1.item_type", "t1.item_id", "t1.item_description", "t1.parent_type", "t1.parent_id", "t1.parent_description", "t1.queued_on", "t2.token")
                .InnerJoin("push_device t2", Exp.EqColumns("t1.device_id", "t2.id"))
                .Where("t2.tenant_id", tenantID)
                .Where("t2.user_id", userID)
                .Where("t2.token", deviceToken);

            if (from > DateTime.MinValue)
                selectQuery.Where(Exp.Gt("t1.queued_on", from));

            if (to < DateTime.MaxValue)
                selectQuery.Where(Exp.Lt("t1.queued_on", to));

            using (var db = GetDbManager())
            {
                return db.ExecuteList(
                    selectQuery,
                    r =>
                        {
                            var notification = new PushNotification
                                {
                                    Module = (PushModule)r.Get<int>("module"),
                                    Action = (PushAction)r.Get<int>("action"),
                                    Item = new PushItem
                                        {
                                            Type = (PushItemType)r.Get<int>("item_type"),
                                            ID = r.Get<string>("item_id"),
                                            Description = r.Get<string>("item_description")
                                        },
                                    QueuedOn = r.Get<DateTime>("queued_on")
                                };

                            if (!r.IsDBNull(r.GetOrdinal("parent_type")))
                                notification.ParentItem = new PushItem
                                    {
                                        Type = (PushItemType)r.Get<int>("parent_type"),
                                        ID = r.Get<string>("parent_id"),
                                        Description = r.Get<string>("parent_description")
                                    };

                            return notification;
                        });
            }
        }
        public void EnqueueNotification(int tenantID, string userID, PushNotification notification, List<string> targetDevices)
        {
            List<Device> sendForDevices = GetDeviceDao().GetAll(tenantID, userID);

            if (targetDevices != null && targetDevices.Any())
            {
                var restrictedDevices = targetDevices.Where(token => sendForDevices.All(device => device.Token != token)).ToList();

                if (restrictedDevices.Any())
                    throw new FaultException(string.Format("can't send for devices ({0})", string.Join("; ", restrictedDevices)));

                sendForDevices = sendForDevices.Where(device => targetDevices.Contains(device.Token)).ToList();
            }

            if (!sendForDevices.Any())
            {
                _log.DebugFormat("no registered devices for user {0}", userID);
                return;
            }

            foreach (var device in sendForDevices)
            {
                if (string.IsNullOrEmpty(notification.Message) && notification.Badge == device.Badge)
                    continue;

                device.Badge = notification.Badge ?? device.Badge + 1;

                try
                {
                    switch (device.Type)
                    {
                        case DeviceType.Ios:
                            EnqueueApnsNotification(device, notification);
                            break;

                        case DeviceType.Android:
                            EnqueueAndroidNotification(device, notification);
                            break;
                    }
                }
                catch (Exception error)
                {
                    _log.WarnFormat("enqueue notification {0} for device {1} error: {2}", notification.Message, device.ID, error);
                }
                
                GetDeviceDao().Save(device);

                if (notification.Module != PushModule.Unknown)
                    GetNotificationDao().Save(device.ID, notification);
            }
        }
        private void EnqueueAndroidNotification(Device device, PushNotification notification)
        {
            var config = PushServiceConfiguration.GetSection();

            string json = string.Format(@"{{""message"":""{0}"",""msgcnt"":""{1}"",""regid"":""{2}""", notification.Message, device.Badge, device.RegistrationID);
            if (notification.Module != PushModule.Unknown && notification.Item != null)
            {
                var itemType = notification.Item.Type;
                var itemId = notification.Item.ID;

                if (notification.Item.Type == PushItemType.Subtask && notification.ParentItem != null)
                {
                    itemType = notification.ParentItem.Type;
                    itemId = notification.ParentItem.ID;
                }

                json += string.Format(@",""itemid"":""{0}"",""itemtype"":""{1}""", itemId, itemType.ToString().ToLower());
            }

            json += "}";

            var gcmNotification = new GcmNotification()
                .ForDeviceRegistrationId(device.Token)
                .WithJson(json);

            if (config.IsDebug || !config.Gcm.ElementInformation.IsPresent)
            {
                _log.DebugFormat("notification ({0}) prevented from sending to device {1}", gcmNotification, device.ID);
                return;
            }

            PushBrokerProvider.Instance.QueueNotification(gcmNotification);
        }
        private void EnqueueApnsNotification(Device device, PushNotification notification)
        {
            var config = PushServiceConfiguration.GetSection();
            
            string message = notification.Message;

            if (message != null && message.Length > config.Apns.MaxMessageLength)
                message = notification.ShortMessage ?? notification.Message;

            if (message != null && message.Length > config.Apns.MaxMessageLength)
            {
                _log.WarnFormat("message is larger than maximum allowed length of {0}", config.Apns.MaxMessageLength);
                return;
            }

            var apnsNotification = new AppleNotification()
                .ForDeviceToken(device.Token)
                .WithAlert(message)
                .WithBadge(device.Badge)
                .WithCustomItem("regid", device.RegistrationID);

            if (notification.Module != PushModule.Unknown && notification.Item != null)
            {
                var itemType = notification.Item.Type;
                var itemId = notification.Item.ID;

                if (notification.Item.Type == PushItemType.Subtask && notification.ParentItem != null)
                {
                    itemType = notification.ParentItem.Type;
                    itemId = notification.ParentItem.ID;
                }

                apnsNotification.WithCustomItem("itemid", itemId);
                apnsNotification.WithCustomItem("itemtype", itemType.ToString().ToLower());
            }

            if (config.IsDebug || !config.Apns.ElementInformation.IsPresent)
            {
                _log.DebugFormat("notification ({0}) prevented from sending to device {1}", apnsNotification, device.ID);
                return;
            }

            PushBrokerProvider.Instance.QueueNotification(apnsNotification);
        }