-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (114 loc) · 4.32 KB
/
Program.cs
File metadata and controls
134 lines (114 loc) · 4.32 KB
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
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.Toolkit.Uwp.Notifications;
using Newtonsoft.Json;
ToastNotificationManagerCompat.OnActivated += OnToastNotificationActivated;
// Create assets directory
string assetsDirPath = Path.Combine(Environment.CurrentDirectory, "assets");
if (!Directory.Exists(assetsDirPath))
{
Directory.CreateDirectory(assetsDirPath);
}
// Download assets
string messagesAssetFilePath = Path.Combine(assetsDirPath, "logo_messages.png");
DownloadFileAsync(messagesAssetFilePath, "https://ssl.gstatic.com/android-messages-web/images/2022.3/1x/messages_2022_96dp.png");
string phoneAssetFilePath = Path.Combine(assetsDirPath, "logo_phone.png");
DownloadFileAsync(phoneAssetFilePath, "https://cdn-icons-png.flaticon.com/512/7044/7044863.png");
string batteryAssetFilePath = Path.Combine(assetsDirPath, "logo_battery.png");
DownloadFileAsync(batteryAssetFilePath, "https://cdn-icons-png.flaticon.com/512/8514/8514306.png");
// Re-create data directory
string dataDirPath = Path.Combine(Environment.CurrentDirectory, "data");
if (Directory.Exists(dataDirPath))
{
Directory.Delete(dataDirPath, true);
}
Directory.CreateDirectory(dataDirPath);
// Listen for broadcasts
const int port = 5000;
using UdpClient client = new(port);
IPEndPoint endPoint = new(IPAddress.Any, port);
while (true)
{
// Receive data
byte[] data = client.Receive(ref endPoint);
Console.WriteLine($"Received broadcast message from client {endPoint}");
Console.Write("Decoded data is: ");
Console.WriteLine(Encoding.UTF8.GetString(data));
Console.WriteLine();
// Parse data
string json = Encoding.UTF8.GetString(data);
NotificationData notification = JsonConvert.DeserializeObject<NotificationData>(json);
// Write to history file
string dataFilePath = Path.Combine(dataDirPath, string.Join("_", (notification.App + "_" + notification.Title).Replace(' ', '_').Split(Path.GetInvalidFileNameChars())) + ".txt");
bool newLine = File.Exists(dataFilePath);
using (StreamWriter sw = File.AppendText(dataFilePath))
{
sw.Write($"{(newLine ? "\n\n" : "")}[{DateTime.Now:g}]\n{notification.Text}");
}
// Display notification
ToastContentBuilder builder = new();
builder.AddArgument("not_text", notification.Text);
builder.AddArgument("file_path", dataFilePath);
string assetFilePath = notification.App switch
{
"Messages" => messagesAssetFilePath,
"Phone" => phoneAssetFilePath,
"Battery" => batteryAssetFilePath,
_ => null
};
if (assetFilePath != null)
{
builder.AddAppLogoOverride(new Uri(assetFilePath));
}
builder.AddText(notification.App);
builder.AddText(notification.Title);
builder.AddText(notification.Text);
builder.Show();
}
static void OnToastNotificationActivated(ToastNotificationActivatedEventArgsCompat e)
{
Console.WriteLine("Activated: " + e.Argument);
ToastArguments args = ToastArguments.Parse(e.Argument);
string notificationText = args.Get("not_text");
string filePath = args.Get("file_path");
// Open the URL
if (Uri.TryCreate(notificationText, UriKind.Absolute, out Uri uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
{
Process.Start(new ProcessStartInfo(uri.ToString()) { Verb = "open", UseShellExecute = true});
}
// Open the history file
else if (File.Exists(filePath) && Path.GetExtension(filePath) == ".txt")
{
Process.Start(new ProcessStartInfo(filePath) {Verb = "open", UseShellExecute = true});
}
}
static async void DownloadFileAsync(string fileName, string url, bool replace = false)
{
try
{
if (!replace && File.Exists(fileName))
{
return;
}
using HttpClient httpClient = new();
await using Stream s = await httpClient.GetStreamAsync(url);
await using FileStream fs = new(fileName!, FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
internal readonly struct NotificationData
{
[JsonProperty("not_app")]
public readonly string App;
[JsonProperty("not_title")]
public readonly string Title;
[JsonProperty("not_text")]
public readonly string Text;
}