예제 #1
0
        private void workerMethod()
        {
            while (!disposing && !closing)
            {
                try
                {
                    while (this.notifications.Count > 0 && !disposing)
                    {
                        Notification notification = this.notifications.Dequeue();

                        int  tries = 0;
                        bool sent  = false;

                        while (!sent && tries < this.SendRetries)
                        {
                            try
                            {
                                if (!disposing)
                                {
                                    apnsChannel.EnsureConnection();

                                    try
                                    {
                                        apnsChannel.Send(notification);
                                    }
                                    catch (BadDeviceTokenException btex)
                                    {
                                        var onBadDeviceToken = BadDeviceToken;
                                        if (onBadDeviceToken != null)
                                        {
                                            onBadDeviceToken(this, btex);
                                        }
                                    }
                                    catch (NotificationLengthException nlex)
                                    {
                                        var onNotificationTooLong = NotificationTooLong;
                                        if (onNotificationTooLong != null)
                                        {
                                            onNotificationTooLong(this, nlex);
                                        }
                                    }

                                    var onNotificationSuccess = NotificationSuccess;
                                    if (onNotificationSuccess != null)
                                    {
                                        onNotificationSuccess(this, notification);
                                    }

                                    sent = true;
                                }
                                else
                                {
                                    apnsChannel.ForceReconnect();
                                }
                            }
                            catch (Exception ex)
                            {
                                var onError = Error;
                                if (onError != null)
                                {
                                    onError(this, ex);
                                }

                                apnsChannel.ForceReconnect();
                            }

                            tries++;
                        }

                        //Didn't send in 3 tries
                        var onNotificationFailed = NotificationFailed;
                        if (!sent && onNotificationFailed != null)
                        {
                            onNotificationFailed(this, notification);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var onError = Error;
                    if (onError != null)
                    {
                        onError(this, ex);
                    }

                    apnsChannel.ForceReconnect();
                }

                if (!disposing)
                {
                    Thread.Sleep(500);
                }
            }
        }