Skip to content

Commit 3e5298d

Browse files
committed
Send toast notifications
But only when app is not in foreground. This does not handle sending notifications when app is suspended yet.
1 parent b1e971c commit 3e5298d

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

Signal-Windows/App.xaml.cs

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Windows.UI.Xaml;
1313
using Windows.UI.Xaml.Controls;
1414
using Windows.UI.Xaml.Navigation;
15+
using Microsoft.QueryStringDotNET;
1516

1617
namespace Signal_Windows
1718
{
@@ -28,6 +29,7 @@ sealed partial class App : Application
2829
public static bool MainPageActive = false;
2930
public static string USER_AGENT = "Signal-Windows";
3031
public static uint PREKEY_BATCH_SIZE = 100;
32+
public static bool WindowActive = false;
3133
private Task<SignalStore> Init;
3234

3335
/// <summary>
@@ -60,8 +62,29 @@ public App()
6062
/// </summary>
6163
/// <param name="e">Details über Startanforderung und -prozess.</param>
6264
protected override async void OnLaunched(LaunchActivatedEventArgs e)
65+
{
66+
await OnLaunchedOrActivated(e);
67+
}
68+
69+
protected override async void OnActivated(IActivatedEventArgs args)
70+
{
71+
await OnLaunchedOrActivated(args, false);
72+
}
73+
74+
/// <summary>
75+
///
76+
/// </summary>
77+
/// <param name="e"></param>
78+
/// <param name="launched">
79+
/// If OnLaunched this is true
80+
/// If OnActivated this is false
81+
/// </param>
82+
/// <returns></returns>
83+
private async Task OnLaunchedOrActivated(IActivatedEventArgs e, bool launched = true)
6384
{
6485
Debug.WriteLine("Signal-Windows " + LocalFolder.Path.ToString());
86+
Window.Current.Activated += Current_Activated;
87+
WindowActive = true;
6588
Frame rootFrame = Window.Current.Content as Frame;
6689

6790
// App-Initialisierung nicht wiederholen, wenn das Fenster bereits Inhalte enthält.
@@ -82,25 +105,53 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
82105
Window.Current.Content = rootFrame;
83106
}
84107

85-
if (e.PrelaunchActivated == false)
108+
if (launched)
86109
{
87-
if (rootFrame.Content == null)
110+
LaunchActivatedEventArgs args = e as LaunchActivatedEventArgs;
111+
if (args.PrelaunchActivated == false)
88112
{
89-
// Wenn der Navigationsstapel nicht wiederhergestellt wird, zur ersten Seite navigieren
90-
// und die neue Seite konfigurieren, indem die erforderlichen Informationen als Navigationsparameter
91-
// übergeben werden
92-
Store = await Init;
93-
if (Store == null || !Store.Registered)
113+
if (rootFrame.Content == null)
94114
{
95-
rootFrame.Navigate(typeof(StartPage), e.Arguments);
115+
// Wenn der Navigationsstapel nicht wiederhergestellt wird, zur ersten Seite navigieren
116+
// und die neue Seite konfigurieren, indem die erforderlichen Informationen als Navigationsparameter
117+
// übergeben werden
118+
Store = await Init;
119+
if (Store == null || !Store.Registered)
120+
{
121+
rootFrame.Navigate(typeof(StartPage), args.Arguments);
122+
}
123+
else
124+
{
125+
rootFrame.Navigate(typeof(MainPage), args.Arguments);
126+
}
96127
}
97-
else
128+
}
129+
}
130+
else
131+
{
132+
if (e is ToastNotificationActivatedEventArgs)
133+
{
134+
var args = e as ToastNotificationActivatedEventArgs;
135+
QueryString queryString = QueryString.Parse(args.Argument);
136+
if (!(rootFrame.Content is MainPage))
98137
{
99-
rootFrame.Navigate(typeof(MainPage), e.Arguments);
138+
rootFrame.Navigate(typeof(MainPage), queryString);
100139
}
101140
}
102-
// Sicherstellen, dass das aktuelle Fenster aktiv ist
103-
Window.Current.Activate();
141+
}
142+
// Sicherstellen, dass das aktuelle Fenster aktiv ist
143+
Window.Current.Activate();
144+
}
145+
146+
private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
147+
{
148+
if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
149+
{
150+
WindowActive = false;
151+
}
152+
else
153+
{
154+
WindowActive = true;
104155
}
105156
}
106157

Signal-Windows/Signal/IncomingMessages.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using System;
1010
using System.Collections.Generic;
1111
using System.Diagnostics;
12+
using Microsoft.Toolkit.Uwp.Notifications;
13+
using Windows.UI.Notifications;
14+
using Microsoft.QueryStringDotNET;
1215

1316
namespace Signal_Windows.ViewModels
1417
{
@@ -421,10 +424,60 @@ private void HandleSignalMessage(SignalServiceEnvelope envelope, SignalServiceCo
421424
}
422425
}
423426
Debug.WriteLine("received message: " + message.Content);
427+
if (!App.WindowActive)
428+
{
429+
SendMessageNotification(message);
430+
}
424431
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
425432
{
426433
await UIHandleIncomingMessage(message);
427434
}).AsTask().Wait();
428435
}
436+
437+
private void SendMessageNotification(SignalMessage message)
438+
{
439+
string notificationId = new QueryString()
440+
{
441+
{ "threadId", message.ThreadId },
442+
{ "recievedTimestamp", message.ReceivedTimestamp.ToString() }
443+
}.ToString();
444+
ToastBindingGeneric toastBinding = new ToastBindingGeneric()
445+
{
446+
AppLogoOverride = new ToastGenericAppLogo()
447+
{
448+
Source = "ms-appx:///Assets/gambino.png",
449+
HintCrop = ToastGenericAppLogoCrop.Circle
450+
}
451+
};
452+
AdaptiveText title = new AdaptiveText()
453+
{
454+
Text = message.Author.ThreadDisplayName,
455+
HintMaxLines = 1
456+
};
457+
AdaptiveText messageText = new AdaptiveText()
458+
{
459+
Text = message.Content.Content
460+
};
461+
toastBinding.Children.Add(title);
462+
toastBinding.Children.Add(messageText);
463+
464+
ToastContent toastContent = new ToastContent()
465+
{
466+
Launch = notificationId,
467+
Visual = new ToastVisual()
468+
{
469+
BindingGeneric = toastBinding
470+
},
471+
DisplayTimestamp = DateTimeOffset.FromUnixTimeMilliseconds(message.ReceivedTimestamp)
472+
};
473+
474+
ToastNotification toastNotification = new ToastNotification(toastContent.GetXml());
475+
if (message.Author.ExpiresInSeconds > 0)
476+
{
477+
toastNotification.ExpirationTime = DateTime.Now.Add(TimeSpan.FromSeconds(message.Author.ExpiresInSeconds));
478+
}
479+
toastNotification.Tag = notificationId;
480+
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
481+
}
429482
}
430483
}

Signal-Windows/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
"Microsoft.Toolkit.Uwp.Notifications": "1.5.1",
1010
"MvvmLight": "5.3.0",
1111
"Nito.AsyncEx": "4.0.1",
12+
"QueryString.NET": "1.0.0",
1213
"ZXing.Net.Mobile": "2.2.9"
1314
},
1415
"frameworks": {
15-
"uap10.0.10586": {}
16+
"uap10.0.14393": {}
1617
},
1718
"runtimes": {
1819
"win10-arm": {},

0 commit comments

Comments
 (0)