示例#1
0
        public string RegisterDevice(int tenantID, string userID, string token, DeviceType type)
        {
            if (string.IsNullOrEmpty(token))
                throw new FaultException("empty device token");

            if (string.IsNullOrEmpty(userID))
                throw new FaultException("empty user id");

            var device = GetDeviceDao().GetAll(tenantID, userID).FirstOrDefault(x => x.Token == token);

            if (device == null)
            {
                _log.DebugFormat("register device ({0}, {1}, {2}, {3})", tenantID, userID, token, type);
                device = new Device
                    {
                        TenantID = tenantID,
                        UserID = userID,
                        Token = token,
                        Type = type
                    };

                GetDeviceDao().Save(device);
            }

            return device.RegistrationID;
        }
示例#2
0
        public void TokenCrudTest()
        {
            var device1 = new Device {Token = "test1", TenantID = TestTenantID, UserID = TestUserID.ToString()};
            var device2 = new Device {Token = "test2", TenantID = TestTenantID, UserID = TestUserID.ToString()};

            _deviceDao.Save(device1);
            _deviceDao.Save(device2);

            var devices = _deviceDao.GetAll(TestTenantID, TestUserID.ToString());
            Assert.AreEqual(2, devices.Count);
            Assert.That(devices.Any(device => device.Token == "test1"));
            Assert.That(devices.Any(device => device.Token == "test2"));

            _deviceDao.UpdateToken("test1", "test1-renamed");

            device2.Badge = 5;
            _deviceDao.Save(device2);

            devices = _deviceDao.GetAll(TestTenantID, TestUserID.ToString());
            Assert.AreEqual(2, devices.Count);
            Assert.That(devices.All(device => device.Token != "test1"));
            Assert.That(devices.Any(device => device.Token == "test1-renamed"));
            Assert.AreEqual(5, devices.First(device => device.Token == "test2").Badge);

            _deviceDao.Delete("test1-renamed");
            _deviceDao.Delete("test2");

            devices = _deviceDao.GetAll(TestTenantID, TestUserID.ToString());
            Assert.AreEqual(0, devices.Count);
        }
示例#3
0
 public void Save(Device device)
 {
     using (var db = GetDbManager())
     {
         device.ID = db.ExecuteScalar<int>(
             new SqlInsert("push_device")
                 .ReplaceExists(true)
                 .Identity(0, 0, returnIdentity: true)
                 .InColumnValue("id", device.ID)
                 .InColumnValue("tenant_id", device.TenantID)
                 .InColumnValue("user_id", device.UserID)
                 .InColumnValue("token", device.Token)
                 .InColumnValue("type", device.Type)
                 .InColumnValue("badge", device.Badge));
     }
 }
示例#4
0
        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);
        }
示例#5
0
        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);
        }