-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
136 lines (114 loc) · 4.6 KB
/
MainPage.xaml.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Diagnostics;
using System.Linq;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Metadata;
using Windows.Media.Devices;
using Windows.UI.Notifications;
using Windows.UI.Notifications.Management;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Notify
{
public sealed partial class MainPage : Page
{
UserNotificationListener listener;
public static TorchControl torchControl;
public static Frame frame;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
frame = this.Frame;
}
public void Initialize()
{
if (ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener"))
{
Debug.WriteLine("API уведомлений поддерживаются");
StatusUpdate("Выполняется настройка");
SetListener();
Ring.IsActive = true;
}
else StatusUpdate("API не поддерживается");
}
public async void SetListener()
{
listener = UserNotificationListener.Current;
UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
switch (accessStatus)
{
case UserNotificationListenerAccessStatus.Allowed:
GetAccessBackgroundTask();
Debug.WriteLine("Уведомления доступны");
break;
case UserNotificationListenerAccessStatus.Denied:
StatusUpdate("Уведомления недоступны. Откройте");
break;
case UserNotificationListenerAccessStatus.Unspecified:
Debug.WriteLine("Неизвестно");
break;
default:
Status.Text = "Уведомления недоступны";
break;
}
}
public async void GetAccessBackgroundTask()
{
BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
switch (backgroundStatus)
{
case BackgroundAccessStatus.AllowedSubjectToSystemPolicy:
Debug.WriteLine("Фоновые задачи доступны частично");
BackgroundRegister();
break;
case BackgroundAccessStatus.AlwaysAllowed:
Debug.WriteLine("Фоновые задачи полностью поддерживаются");
BackgroundRegister();
break;
case BackgroundAccessStatus.DeniedBySystemPolicy:
Debug.WriteLine("Фоновые задачи отключены или недоступны");
break;
case BackgroundAccessStatus.DeniedByUser:
Status.Text = "Фоновые задачи недоступны";
break;
default:
Status.Text = "Фоновые задачи недоступны";
break;
}
}
private void BackgroundRegister()
{
if (!BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals("UserNotificationChanged")))
{
var builder = new BackgroundTaskBuilder()
{
Name = "UserNotificationChanged"
};
builder.SetTrigger(new UserNotificationChangedTrigger(NotificationKinds.Toast));
Debug.WriteLine("Триггер установлен");
builder.Register();
Debug.WriteLine("Задача зарегистрирована");
IconChange.Begin();
}
}
private void OnSettings(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(View.Settings));
}
private void OnAbout(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(View.About));
}
private void SetTask(object sender, RoutedEventArgs e)
{
Initialize();
}
private void StatusUpdate(string message)
{
TextShow.Begin();
Status.Text = message;
}
}
}