-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsToastUtils.cs
95 lines (78 loc) · 2.93 KB
/
WindowsToastUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Threading;
using Windows.Foundation;
using Windows.UI.Notifications;
using Windows.UI.Notifications.Management;
namespace acer_rgb
{
public static class WindowsToastUtils
{
public static bool RequestAccess(out bool denied)
{
bool ret = false;
denied = false;
// Get the listener
UserNotificationListener listener = UserNotificationListener.Current;
// And request access to the user's notifications (must be called from UI thread)
var t = listener.RequestAccessAsync();
UserNotificationListenerAccessStatus accessStatus;
try
{
while (t.Status != AsyncStatus.Completed) Thread.Sleep(10);
accessStatus = t.GetResults();
}
finally
{
t.Close();
}
switch (accessStatus)
{
// This means the user has granted access.
case UserNotificationListenerAccessStatus.Allowed:
// Yay! Proceed as normal
ret = true;
break;
// This means the user has denied access.
// Any further calls to RequestAccessAsync will instantly
// return Denied. The user must go to the Windows settings
// and manually allow access.
case UserNotificationListenerAccessStatus.Denied:
// Show UI explaining that listener features will not
// work until user allows access.
denied = true;
break;
// This means the user closed the prompt without
// selecting either allow or deny. Further calls to
// RequestAccessAsync will show the dialog again.
case UserNotificationListenerAccessStatus.Unspecified:
// Show UI that allows the user to bring up the prompt again
break;
}
return ret;
}
public static int GetToastCount()
{
int ret = 0;
try
{
// Get the listener
UserNotificationListener listener = UserNotificationListener.Current;
var n = listener.GetNotificationsAsync(NotificationKinds.Toast);
IReadOnlyList<UserNotification> l;
try
{
while (n.Status != AsyncStatus.Completed) Thread.Sleep(10);
l = n.GetResults();
ret = l.Count;
}
finally
{
n.Close();
}
}
catch { }
return ret;
}
}
}