Ejemplo n.º 1
0
        public async Task Send(ApnsHttp2Notification notification)
        {
            var url = string.Format("https://{0}:{1}/3/device/{2}",
                                    Configuration.Host,
                                    Configuration.Port,
                                    notification.DeviceToken);
            var uri = new Uri(url);

            var payload = notification.Payload.ToString();

            var data = Encoding.UTF8.GetBytes(payload);

            var headers = new NameValueCollection();

            headers.Add("apns-id", notification.Uuid);  // UUID

            if (notification.Expiration.HasValue)
            {
                var sinceEpoch        = notification.Expiration.Value.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                var secondsSinceEpoch = (long)sinceEpoch.TotalSeconds;
                headers.Add("apns-expiration", secondsSinceEpoch.ToString());   //Epoch in seconds
            }

            if (notification.Priority.HasValue)
            {
                headers.Add("apns-priority", notification.Priority == ApnsPriority.Low ? "5" : "10");  // 5 or 10
            }
            headers.Add("content-length", data.Length.ToString());

            if (!string.IsNullOrEmpty(notification.Topic))
            {
                headers.Add("apns-topic", notification.Topic);  // string topic
            }
            Http2Client.Http2Response response;
            lock (_syncObj)
            {
                response = http2.Post(uri, headers, data).Result;
            }

            if (response.Status == HttpStatusCode.OK)
            {
                // Check for matching uuid's
                var responseUuid = response.Headers ["apns-id"];
                if (responseUuid != notification.Uuid)
                {
                    throw new Exception("Mismatched APNS-ID header values");
                }
            }
            else
            {
                // Try parsing json body
                var json = new JObject();

                if (response.Body != null && response.Body.Length > 0)
                {
                    var body = Encoding.ASCII.GetString(response.Body);
                    json = JObject.Parse(body);
                }

                if (response.Status == HttpStatusCode.Gone)
                {
                    var timestamp = DateTime.UtcNow;
                    if (json != null && json["timestamp"] != null)
                    {
                        var sinceEpoch = json.Value <long> ("timestamp");
                        timestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(sinceEpoch);
                    }

                    // Expired
                    throw new PushSharp.Core.DeviceSubscriptonExpiredException(notification)
                          {
                              OldSubscriptionId = notification.DeviceToken,
                              NewSubscriptionId = null,
                              ExpiredAt         = timestamp
                          };
                }

                // Get the reason
                var reasonStr = json.Value <string> ("reason");

                var reason = (ApnsHttp2FailureReason)Enum.Parse(typeof(ApnsHttp2FailureReason), reasonStr, true);

                throw new ApnsHttp2NotificationException(reason, notification);
            }
        }
Ejemplo n.º 2
0
 public ApnsHttp2NotificationException(ApnsHttp2FailureReason reason, ApnsHttp2Notification notification, Exception innerException)
     : base($"Apns notification error: '{reason}'", notification, innerException)
 {
     ErrorStatusCode = reason;
     Notification    = notification;
 }
Ejemplo n.º 3
0
 public ApnsHttp2NotificationException(ApnsHttp2FailureReason reason, ApnsHttp2Notification notification)
     : base(reason.ToString())
 {
     Notification = notification;
     Reason       = reason;
 }
Ejemplo n.º 4
0
        public async Task Send (ApnsHttp2Notification notification)
        {
            var url = string.Format ("https://{0}:{1}/3/device/{2}", 
                          Configuration.Host,
                          Configuration.Port,
                          notification.DeviceToken);            
            var uri = new Uri (url);

            var payload = notification.Payload.ToString ();

            var data = Encoding.ASCII.GetBytes (payload);

            var headers = new NameValueCollection ();
            headers.Add ("apns-id", notification.Uuid); // UUID

            if (notification.Expiration.HasValue) {
                var sinceEpoch = notification.Expiration.Value.ToUniversalTime () - new DateTime (1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                var secondsSinceEpoch = (long)sinceEpoch.TotalSeconds;
                headers.Add ("apns-expiration", secondsSinceEpoch.ToString ()); //Epoch in seconds
            }

            if (notification.Priority.HasValue)
                headers.Add ("apns-priority", notification.Priority == ApnsPriority.Low ? "5" : "10"); // 5 or 10

            headers.Add ("content-length", data.Length.ToString ());

            if (!string.IsNullOrEmpty (notification.Topic)) 
                headers.Add ("apns-topic", notification.Topic); // string topic

            var response = await http2.Post (uri, headers, data);
            
            if (response.Status == HttpStatusCode.OK) {
                // Check for matching uuid's
                var responseUuid = response.Headers ["apns-id"];
                if (responseUuid != notification.Uuid)
                    throw new Exception ("Mismatched APNS-ID header values");
            } else {
                // Try parsing json body
                var json = new JObject ();

                if (response.Body != null && response.Body.Length > 0) {
                    var body = Encoding.ASCII.GetString (response.Body);
                    json = JObject.Parse (body);
                }

                if (response.Status == HttpStatusCode.Gone) {

                    var timestamp = DateTime.UtcNow;
                    if (json != null && json["timestamp"] != null) {
                        var sinceEpoch = json.Value<long> ("timestamp");
                        timestamp = new DateTime (1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds (sinceEpoch);
                    }

                    // Expired
                    throw new PushSharp.Core.DeviceSubscriptonExpiredException {
                        OldSubscriptionId = notification.DeviceToken,
                        NewSubscriptionId = null,
                        ExpiredAt = timestamp
                    };
                }

                // Get the reason
                var reasonStr = json.Value<string> ("reason");

                var reason = (ApnsHttp2FailureReason)Enum.Parse (typeof (ApnsHttp2FailureReason), reasonStr, true);

                throw new ApnsHttp2NotificationException (reason, notification);
            }
        }
Ejemplo n.º 5
0
        public async Task Send(ApnsHttp2Notification notification)
        {
            var url = string.Format("/3/device/{0}", notification.DeviceToken);
            var uri = new Uri(url);

            var payload = notification.Payload.ToString();
            var content = new StringContent(payload, Encoding.UTF8, "application/json");

            content.Headers.Add("apns-id", notification.Uuid); // UUID

            if (notification.Expiration.HasValue)
            {
                var sinceEpoch        = notification.Expiration.Value.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                var secondsSinceEpoch = (long)sinceEpoch.TotalSeconds;
                content.Headers.Add("apns-expiration", secondsSinceEpoch.ToString()); //Epoch in seconds
            }

            if (notification.Priority.HasValue)
            {
                content.Headers.Add("apns-priority", notification.Priority == ApnsPriority.Low ? "5" : "10"); // 5 or 10
            }
            if (!string.IsNullOrEmpty(notification.Topic))
            {
                content.Headers.Add("apns-topic", notification.Topic); // string topic
            }
            var response = await httpClient.PostAsync(uri, content);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                // Check for matching uuid's
                var responseUuid = response.Headers.GetValues("apns-id").FirstOrDefault();
                if (responseUuid != notification.Uuid)
                {
                    throw new Exception("Mismatched APNS-ID header values");
                }
            }
            else
            {
                // Try parsing json body
                var json = new JObject();

                if (response.Content != null)
                {
                    var body = await response.Content.ReadAsStringAsync();

                    json = JObject.Parse(body);
                }

                if (response.StatusCode == HttpStatusCode.Gone)
                {
                    var timestamp = DateTime.UtcNow;
                    if (json != null && json["timestamp"] != null)
                    {
                        var sinceEpoch = json.Value <long> ("timestamp");
                        timestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(sinceEpoch);
                    }

                    // Expired
                    throw new PushSharp.Core.DeviceSubscriptionExpiredException(notification)
                          {
                              OldSubscriptionId = notification.DeviceToken,
                              NewSubscriptionId = null,
                              ExpiredAt         = timestamp
                          };
                }

                // Get the reason
                var reasonStr = json.Value <string> ("reason");

                var reason = (ApnsHttp2FailureReason)Enum.Parse(typeof(ApnsHttp2FailureReason), reasonStr, true);

                throw new ApnsHttp2NotificationException(reason, notification);
            }
        }