Exemple #1
0
        protected override void OnNotify(NotificationEventArgs e)
        {
            if (_client == null || !_client.Connected)
            {
                Connect(Settings.Apns.GatewayHost, ref _client, ref _stream);
                _stream.BeginRead(_readBuffer, 0, _readBuffer.Length, OnRead, null);
            }

            var payloadJson   = JsonConvert.SerializeObject(e.Payload);
            var payloadLength = Encoding.UTF8.GetByteCount(payloadJson);

            if (payloadLength > MaxPayloadLength)
            {
                throw new UndeliverableException("Payload too large.");
            }
            var buf = new byte[45 + payloadLength];

            try
            {
                DeviceToken.Validate(Platform.Apns, e.DeviceToken);
            }
            catch (ValidationException)
            {
                throw new UndeliverableException("Invalid device token.");
            }

            FormattingExtensions.FromHexString(e.DeviceToken);

            buf[0] = 1;                                    // command (always 1)
            (_curMessageId++).ToBytesNetworkOrder(buf, 1); // incrementing reference number
            e.Expiry.ToBytesNetworkOrder(buf, 5);          // UNIX epoch expiry

            // token length and token
            buf[9]  = 0;
            buf[10] = 32; // length (always 32)
            FormattingExtensions.FromHexString(e.DeviceToken, buf, 11);

            // payload length and payload
            ((short)payloadLength).ToBytesNetworkOrder(buf, 43);
            Encoding.UTF8.GetBytes(payloadJson, 0, payloadJson.Length, buf, 45);

            _stream.Write(buf);
        }
Exemple #2
0
 public void Validate()
 {
     DeviceToken.Validate(this.Platform, this.Token);
     this.Token = DeviceToken.Normalize(this.Platform, this.Token);
 }
        public void Validate()
        {
            if (this.Tokens != null)
            {
                foreach (var tok in this.Tokens)
                {
                    DeviceToken.Validate(this.Platform, tok);
                }
            }
            if (this.Segments != null)
            {
                foreach (var seg in this.Segments)
                {
                    if (string.IsNullOrWhiteSpace(seg))
                    {
                        throw new ValidationException("Invalid segment.");
                    }
                }
            }

            switch (this.Platform)
            {
            case Platform.Gcm:
                break;

            case Platform.Mpns:
            {
                var payload = this.Payload.ToObject <MpnsPayload>();
                if (payload.Headers == null)
                {
                    throw new ValidationException("Headers collection required");
                }
                foreach (var header in payload.Headers)
                {
                    if (header.Value == null || !header.Key.StartsWith("X-"))
                    {
                        throw new ValidationException("Invalid custom headers.");
                    }
                }
                if (string.IsNullOrEmpty(payload.Body))
                {
                    throw new ValidationException("Message body is required.");
                }
            }
            break;

            case Core.Platform.Wns:
                break;

            case Platform.Apns:
            {
                var payload = JsonConvert.SerializeObject(this.Payload);
                if (payload.Length > MaxApplePayloadSize)
                {
                    throw new ValidationException("Payload too large.");
                }
            }
            break;

            default:
                throw new ValidationException("Invalid platform.");
            }

            if (this.Platform == 0)
            {
                throw new ValidationException("Platform is required.");
            }
        }