コード例 #1
0
        /// <summary>
        /// Updates all of the notifications based on the message list.
        /// </summary>
        /// <param name="newMessages"></param>
        public async void UpdateNotifications(List<Message> newMessages)
        {
            bool updateSliently = !m_baconMan.IsBackgroundTask;

            // Check if we are disabled.
            if (!IsEnabled || !m_baconMan.UserMan.IsUserSignedIn)
            {
                // Clear things out
                ToastNotificationManager.History.Clear();
                // Clear the tile
                BadgeNumericNotificationContent content = new BadgeNumericNotificationContent();
                content.Number = 0;
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(new BadgeNotification(content.GetXml()));
                return;
            }

            try
            {
                // Get the current state of the messages.
                IReadOnlyList<ToastNotification> history = ToastNotificationManager.History.GetHistory();

                // We need to keep track of anything new to send to the band.
                List<Tuple<string, string, string>> newNotifications = new List<Tuple<string, string, string>>();

                // We also need to keep track of how many are unread so we can updates our badge.
                int unreadCount = 0;

                if (NotificationType == 0)
                {
                    // For help look here.
                    // http://blogs.msdn.com/b/tiles_and_toasts/archive/2015/07/09/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10.aspx

                    // We need to keep track of what notifications we should have so we can remove ones we don't need.
                    Dictionary<string, bool> unReadMessages = new Dictionary<string, bool>();

                    foreach (Message message in newMessages.Reverse<Message>())
                    {
                        // If it is new
                        if (message.IsNew)
                        {
                            unreadCount++;

                            // Add the message to our list
                            unReadMessages.Add(message.Id, true);

                            // Check if it is already been reported but dismissed from the UI
                            if (ShownMessageNotifications.ContainsKey(message.Id))
                            {
                                continue;
                            }

                            // If not add that we are showing it.
                            ShownMessageNotifications[message.Id] = true;

                            // Check if it is already in the UI
                            foreach (ToastNotification oldToast in history)
                            {
                                if (oldToast.Tag.Equals(message.Id))
                                {
                                    continue;
                                }
                            }

                            // Get the post title.
                            string title = "";
                            if (message.WasComment)
                            {
                                string subject = message.Subject;
                                if (subject.Length > 0)
                                {
                                    subject = subject.Substring(0, 1).ToUpper() + subject.Substring(1);
                                }
                                title = subject + " from " + message.Author;
                            }
                            else
                            {
                                title = message.Subject;
                            }

                            // Get the body
                            string body = message.Body;

                            // Add the notification to our list
                            newNotifications.Add(new Tuple<string, string, string>(title, body, message.Id));
                        }
                    }

                    // Make sure that all of the messages in our history are still unread
                    // if not remove them.
                    for(int i = 0; i < history.Count; i++)
                    {
                        if(!unReadMessages.ContainsKey(history[i].Tag))
                        {
                            // This message isn't unread any longer.
                            ToastNotificationManager.History.Remove(history[i].Tag);
                        }
                    }

                    // Save any settings we changed
                    SaveSettings();
                }
                else
                {
                    // Count how many are unread
                    foreach (Message message in newMessages)
                    {
                        if (message.IsNew)
                        {
                            unreadCount++;
                        }
                    }

                    // If we have a different unread count now show the notification.
                    if(LastKnownUnreadCount != unreadCount)
                    {
                        // Update the notification.
                        LastKnownUnreadCount = unreadCount;

                        // Clear anything we have in the notification center already.
                        ToastNotificationManager.History.Clear();

                        if (unreadCount != 0)
                        {
                            newNotifications.Add(new Tuple<string, string, string>($"You have {unreadCount} new inbox message" + (unreadCount == 1 ? "." : "s."), "", "totalCount"));
                        }
                    }              
                }

                // For every notification, show it.
                bool hasShownNote = false;
                foreach(Tuple<string, string, string> newNote in newNotifications)
                {
                    // Make the visual
                    ToastVisual visual = new ToastVisual();
                    visual.TitleText = new ToastText() { Text = newNote.Item1 };
                    if(!String.IsNullOrWhiteSpace(newNote.Item2))
                    {
                        visual.BodyTextLine1 = new ToastText() { Text = newNote.Item2 };
                    }

                    // Make the toast content
                    ToastContent toastContent = new ToastContent();
                    toastContent.Visual = visual;
                    toastContent.Launch = c_messageInboxOpenArgument;
                    toastContent.ActivationType = ToastActivationType.Foreground;
                    toastContent.Duration = ToastDuration.Short;

                    var toast = new ToastNotification(toastContent.GetXml());
                    toast.Tag = newNote.Item3;

                    // Only show if we should and this is the first message to show.
                    toast.SuppressPopup = hasShownNote || updateSliently || AddToNotificationCenterSilently;
                    ToastNotificationManager.CreateToastNotifier().Show(toast);

                    // Mark that we have shown one.
                    hasShownNote = true;
                }

                // Make sure the main tile is an iconic tile.
                m_baconMan.TileMan.EnsureMainTileIsIconic();

                // Update the badge
                BadgeNumericNotificationContent content = new BadgeNumericNotificationContent();
                content.Number = (uint)unreadCount;
                BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(new BadgeNotification(content.GetXml()));

                // Update the band if we have one.
                if(!updateSliently)
                {
                    await m_baconMan.BackgroundMan.BandMan.UpdateInboxMessages(newNotifications, newMessages);
                }

                // If all was successful update the last time we updated
                LastUpdateTime = DateTime.Now;
            }
            catch (Exception ex)
            {
                m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "messageUpdaterFailed", ex);
                m_baconMan.MessageMan.DebugDia("failed to update message notifications", ex);
            }

            // When we are done release the deferral
            ReleaseDeferal();
        }
コード例 #2
0
ファイル: TileManager.cs プロジェクト: Arthur-Lee/Baconit
        /// <summary>
        /// Makes sure the main app tile is an icon tile type
        /// </summary>
        public void UpdateMainTile(int unreadCount)
        {
            // Setup the main tile as iconic for small and medium
            TileContent content = new TileContent()
            {
                Visual = new TileVisual()
                {
                    TileSmall = new TileBinding()
                    {
                        Content = new TileBindingContentIconic()
                        {
                            Icon = new TileImageSource("ms-appx:///Assets/AppAssets/IconicTiles/Iconic144.png"),
                        }
                    },
                    TileMedium = new TileBinding()
                    {
                        Content = new TileBindingContentIconic()
                        {
                            Icon = new TileImageSource("ms-appx:///Assets/AppAssets/IconicTiles/Iconic200.png"),
                        }
                    },                    
                }
            };

            // If the user is signed in we will do more for large and wide.
            if(m_baconMan.UserMan.IsUserSignedIn && m_baconMan.UserMan.CurrentUser != null && !String.IsNullOrWhiteSpace(m_baconMan.UserMan.CurrentUser.Name))
            {
                content.Visual.TileWide = new TileBinding()
                {
                    Content = new TileBindingContentAdaptive()
                    {
                        Children =
                        {
                            new TileText()
                            {
                                Text = m_baconMan.UserMan.CurrentUser.Name,
                                Style = TileTextStyle.Caption
                            },
                            new TileText()
                            {
                                Text = String.Format("{0:N0}", m_baconMan.UserMan.CurrentUser.CommentKarma) + " comment karma",
                                Style = TileTextStyle.CaptionSubtle
                            },
                            new TileText()
                            {
                                Text = String.Format("{0:N0}", m_baconMan.UserMan.CurrentUser.LinkKarma) + " link karma",
                                Style = TileTextStyle.CaptionSubtle
                            },                           
                        }
                    },
                };

                // If we have messages replace the user name with the message string.
                if (unreadCount != 0)
                {
                    TileText unreadCountText = new TileText()
                    {
                        Text = unreadCount + " Unread Inbox Message" + (unreadCount == 1 ? "" : "s"),
                        Style = TileTextStyle.Body
                    };
                    ((TileBindingContentAdaptive)content.Visual.TileWide.Content).Children[0] = unreadCountText;
                }

                // Also set the cake day if it is today
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                DateTime userCreationTime = origin.AddSeconds(m_baconMan.UserMan.CurrentUser.CreatedUtc).ToLocalTime();
                TimeSpan elapsed = DateTime.Now - userCreationTime;
                double fullYears = Math.Floor((elapsed.TotalDays / 365));
                int daysUntil = (int)(elapsed.TotalDays - (fullYears * 365));

                if(daysUntil == 0)
                {
                    // Make the text
                    TileText cakeDayText = new TileText()
                    {
                        Text = "Today is your cake day!",
                        Style = TileTextStyle.Body
                    };
                    ((TileBindingContentAdaptive)content.Visual.TileWide.Content).Children[0] = cakeDayText;
                }
            }  

            // Set large to the be same as wide.
            content.Visual.TileLarge = content.Visual.TileWide;

            // Update the tile
            TileUpdateManager.CreateTileUpdaterForApplication().Update(new TileNotification(content.GetXml()));

            // Update the badge
            BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent();
            badgeContent.Number = (uint)unreadCount;
            BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(new BadgeNotification(badgeContent.GetXml()));
        }
コード例 #3
0
 /// <summary>
 /// Generates badge notification of goldCount change for receiver.
 /// </summary>
 /// <param name="goldCount">The goldCount of the user.</param>
 /// <returns>String representation of badge object.</returns>
 private string GenerateBadgeNotification(uint goldCount)
 {
     BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent(goldCount);
     return badgeContent.GetContent();
 }
コード例 #4
0
ファイル: Sender.cs プロジェクト: aurora-lzzp/Aurora-Weather
 public static void CreateBadge(BadgeNumericNotificationContent badge)
 {
     BadgeNotification b = new BadgeNotification(badge.GetXml());
     BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(b);
 }
コード例 #5
0
ファイル: SelfwinApp.cs プロジェクト: gleroi/selfnet
 public void UpdateTile(int status)
 {
     var badgeContent = new BadgeNumericNotificationContent((uint)status);
     var badgeNotification = new BadgeNotification(badgeContent.GetXml());
     var updater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
     updater.Update(badgeNotification);
 }