/// <summary>
        /// On subscription change, reset the subscriber tile counter if exist.
        /// </summary>
        protected override void OnSubscribed(SubscriptionEventArgs e)
        {
            // Create a tile message to reset tile count.
            var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
            {
                Count = 0,
                BackgroundImageUri = BackgroundImageUri,
                Title = Title
            };

            tileMsg.SendAsync(e.Subscription.ChannelUri, Log, Log);

            ResetCounter(e.Subscription.UserName);
        }
        private void PushService_TileUpdateRequest(object sender, TileUpdateRequestEventArgs e)
        {
            // Send a tile notification message to the relevant device.
            var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
            {
                BackgroundImageUri = new Uri(string.Format(ImageService.GetTileImageService, e.Parameter))
            };

            tileMsg.SendAsync(e.ChannelUri, Log, Log);
        }
        private void OnRawSent(string userName, MessageSendResult result)
        {
            // In case that the device is disconnected, no need to send a tile message.
            if (result.DeviceConnectionStatus == DeviceConnectionStatus.TempDisconnected)
            {
                return;
            }

            // Checking these three flags we can know what's the state of both the device and apllication.
            bool isApplicationRunning =
                result.SubscriptionStatus == SubscriptionStatus.Active &&
                result.NotificationStatus == NotificationStatus.Received &&
                result.DeviceConnectionStatus == DeviceConnectionStatus.Connected;

            // In case that the application is not running, send a tile update with counter increase.
            if (!isApplicationRunning)
            {
                var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
                {
                    Count = IncreaseCounter(userName),
                    BackgroundImageUri = BackgroundImageUri,
                    Title = Title
                };

                tileMsg.SendAsync(result.ChannelUri, Log, Log);
            }
        }
        private TilePushNotificationMessage GetOrCreateMessage(string userName, bool overrideExisting = true)
        {
            lock (MessageSync)
            {
                TilePushNotificationMessage message;
                if (!_messages.TryGetValue(userName, out message) || overrideExisting)
                {
                    message = new TilePushNotificationMessage(MessageSendPriority.High)
                    {
                        BackgroundImageUri = BackgroundImageUri,
                        Count = Count,
                        Title = Title
                    };

                    _messages[userName] = message;
                }

                return message;
            }
        }
        protected override void OnSend()
        {
            // Starts by sending a tile notification to all relvant subscribers.
            // This tile notification updates the tile with custom image.
            var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
            {
                Count = Count,
                Title = Title
            };

            foreach (var subscriber in PushService.Subscribers)
            {
                // Set the tile background image uri with the address of the ImageService.GetTileImage,
                // REST service, using current subscriber channel uri as a parameter to bo sent to the service.
                tileMsg.BackgroundImageUri = new Uri(string.Format(ImageService.GetTileImageService, string.Empty));
                tileMsg.SendAsync(subscriber.ChannelUri, Log, Log);
            }
        }