1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ #if WINDOWS_UWP
6+
7+ using System . Collections . Generic ;
8+ using Windows . UI . Notifications ;
9+
10+ namespace Microsoft . Toolkit . Uwp . Notifications
11+ {
12+ /// <summary>
13+ /// Allows you to show and schedule toast notifications.
14+ /// </summary>
15+ public sealed class ToastNotifierCompat
16+ {
17+ private ToastNotifier _notifier ;
18+
19+ internal ToastNotifierCompat ( ToastNotifier notifier )
20+ {
21+ _notifier = notifier ;
22+ }
23+
24+ /// <summary>
25+ /// Displays the specified toast notification.
26+ /// </summary>
27+ /// <param name="notification">The object that contains the content of the toast notification to display.</param>
28+ public void Show ( ToastNotification notification )
29+ {
30+ #if WIN32
31+ PreprocessToast ( notification ) ;
32+ #endif
33+
34+ _notifier . Show ( notification ) ;
35+
36+ #if WIN32
37+ ToastNotificationManagerCompat . SetHasSentToastNotification ( ) ;
38+ #endif
39+ }
40+
41+ /// <summary>
42+ /// Hides the specified toast notification from the screen (moves it into Action Center).
43+ /// </summary>
44+ /// <param name="notification">The object that specifies the toast to hide.</param>
45+ public void Hide ( ToastNotification notification )
46+ {
47+ #if WIN32
48+ PreprocessToast ( notification ) ;
49+ #endif
50+
51+ _notifier . Hide ( notification ) ;
52+ }
53+
54+ /// <summary>
55+ /// Adds a ScheduledToastNotification for later display by Windows.
56+ /// </summary>
57+ /// <param name="scheduledToast">The scheduled toast notification, which includes its content and timing instructions.</param>
58+ public void AddToSchedule ( ScheduledToastNotification scheduledToast )
59+ {
60+ #if WIN32
61+ ToastNotificationManagerCompat . PreRegisterIdentityLessApp ( ) ;
62+
63+ PreprocessScheduledToast ( scheduledToast ) ;
64+ #endif
65+
66+ _notifier . AddToSchedule ( scheduledToast ) ;
67+ }
68+
69+ /// <summary>
70+ /// Cancels the scheduled display of a specified ScheduledToastNotification.
71+ /// </summary>
72+ /// <param name="scheduledToast">The notification to remove from the schedule.</param>
73+ public void RemoveFromSchedule ( ScheduledToastNotification scheduledToast )
74+ {
75+ #if WIN32
76+ PreprocessScheduledToast ( scheduledToast ) ;
77+ #endif
78+
79+ _notifier . RemoveFromSchedule ( scheduledToast ) ;
80+ }
81+
82+ /// <summary>
83+ /// Gets the collection of ScheduledToastNotification objects that this app has scheduled for display.
84+ /// </summary>
85+ /// <returns>The collection of scheduled toast notifications that the app bound to this notifier has scheduled for timed display.</returns>
86+ public IReadOnlyList < ScheduledToastNotification > GetScheduledToastNotifications ( )
87+ {
88+ return _notifier . GetScheduledToastNotifications ( ) ;
89+ }
90+
91+ /// <summary>
92+ /// Updates the existing toast notification that has the specified tag and belongs to the specified notification group.
93+ /// </summary>
94+ /// <param name="data">An object that contains the updated info.</param>
95+ /// <param name="tag">The identifier of the toast notification to update.</param>
96+ /// <param name="group">The ID of the ToastCollection that contains the notification.</param>
97+ /// <returns>A value that indicates the result of the update (failure, success, etc).</returns>
98+ public NotificationUpdateResult Update ( NotificationData data , string tag , string group )
99+ {
100+ return _notifier . Update ( data , tag , group ) ;
101+ }
102+
103+ /// <summary>
104+ /// Updates the existing toast notification that has the specified tag.
105+ /// </summary>
106+ /// <param name="data">An object that contains the updated info.</param>
107+ /// <param name="tag">The identifier of the toast notification to update.</param>
108+ /// <returns>A value that indicates the result of the update (failure, success, etc).</returns>
109+ public NotificationUpdateResult Update ( NotificationData data , string tag )
110+ {
111+ #if WIN32
112+ // For apps that don't have identity...
113+ if ( ! DesktopBridgeHelpers . HasIdentity ( ) )
114+ {
115+ // If group isn't specified, we have to add a group since otherwise can't remove without a group
116+ return Update ( data , tag , ToastNotificationManagerCompat . DEFAULT_GROUP ) ;
117+ }
118+ #endif
119+
120+ return _notifier . Update ( data , tag ) ;
121+ }
122+
123+ /// <summary>
124+ /// Gets a value that tells you whether there is an app, user, or system block that prevents the display of a toast notification.
125+ /// </summary>
126+ public NotificationSetting Setting
127+ {
128+ get
129+ {
130+ #if WIN32
131+ // Just like scheduled notifications, apps need to have sent a notification
132+ // before checking the setting value works
133+ ToastNotificationManagerCompat . PreRegisterIdentityLessApp ( ) ;
134+ #endif
135+
136+ return _notifier . Setting ;
137+ }
138+ }
139+
140+ #if WIN32
141+ private void PreprocessToast ( ToastNotification notification )
142+ {
143+ // For apps that don't have identity...
144+ if ( ! DesktopBridgeHelpers . HasIdentity ( ) )
145+ {
146+ // If tag is specified
147+ if ( ! string . IsNullOrEmpty ( notification . Tag ) )
148+ {
149+ // If group isn't specified, we have to add a group since otherwise can't remove without a group
150+ notification . Group = ToastNotificationManagerCompat . DEFAULT_GROUP ;
151+ }
152+ }
153+ }
154+
155+ private void PreprocessScheduledToast ( ScheduledToastNotification notification )
156+ {
157+ // For apps that don't have identity...
158+ if ( ! DesktopBridgeHelpers . HasIdentity ( ) )
159+ {
160+ // If tag is specified
161+ if ( ! string . IsNullOrEmpty ( notification . Tag ) )
162+ {
163+ // If group isn't specified, we have to add a group since otherwise can't remove without a group
164+ notification . Group = ToastNotificationManagerCompat . DEFAULT_GROUP ;
165+ }
166+ }
167+ }
168+ #endif
169+ }
170+ }
171+
172+ #endif
0 commit comments