feat: use Windows 10 notification center api

This commit is contained in:
genteure 2022-08-25 18:44:35 +08:00
parent e2bd7becb0
commit 626e0a5304
4 changed files with 96 additions and 1 deletions

View File

@ -160,6 +160,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="StreamStartedNotification.cs" />
<Compile Include="Update.cs" />
<Compile Include="WorkDirectoryLoader.cs" />
<Compile Include="WpfLogEventSink.cs" />
@ -344,6 +345,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="ModernWpfUI">
<Version>0.9.4</Version>
</PackageReference>
@ -411,4 +415,4 @@
</PropertyGroup>
<Exec Command="$(PkgNuGet_CommandLine)\tools\NuGet pack BililiveRecorder.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(NupkgReleaseDir) -BasePath $(OutDir) -NonInteractive -ForceEnglishOutput" />
</Target>
</Project>
</Project>

View File

@ -273,6 +273,9 @@ You can uninstall me in system settings.", "安装成功 Installed", MessageBoxB
if (!recorder.Config.Global.WpfNotifyStreamStart)
return;
_ = StreamStartedNotification.ShowAsync(room);
return;
_ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
if (Application.Current.MainWindow is NewMainWindow nmw)

View File

@ -14,6 +14,7 @@ using BililiveRecorder.Flv.Pipeline;
using BililiveRecorder.ToolBox;
using Esprima;
using Jint.Runtime;
using Microsoft.Toolkit.Uwp.Notifications;
using Sentry;
using Sentry.Extensibility;
using Serilog;
@ -221,6 +222,12 @@ namespace BililiveRecorder.WPF
}
finally
{
try
{
ToastNotificationManagerCompat.Uninstall();
}
catch (Exception)
{ }
cancel.Cancel();
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
update.WaitForUpdatesOnShutdownAsync().GetAwaiter().GetResult();

View File

@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using BililiveRecorder.Core;
using Microsoft.Toolkit.Uwp.Notifications;
#nullable enable
namespace BililiveRecorder.WPF
{
internal static class StreamStartedNotification
{
public static async Task ShowAsync(IRoom room)
{
var tempPath = Path.Combine(Path.GetTempPath(), "brec-notifi");
Uri? cover = null, face = null;
DateTime? time = null;
try
{
Directory.CreateDirectory(tempPath);
var json = room.RawBilibiliApiJsonData;
var live_start_time = json?["room_info"]?["live_start_time"]?.ToObject<long?>();
if (live_start_time.HasValue && live_start_time > 0)
{
time = DateTimeOffset.FromUnixTimeSeconds(live_start_time.Value).LocalDateTime;
}
var coverUrl = json?["room_info"]?["cover"]?.ToObject<string>();
var faceUrl = json?["anchor_info"]?["base_info"]?["face"]?.ToObject<string>();
var coverFile = Path.Combine(tempPath, Path.GetFileName(coverUrl));
var faceFile = Path.Combine(tempPath, Path.GetFileName(faceUrl));
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.UserAgent.Clear();
if (!string.IsNullOrEmpty(faceUrl))
{
using var faceFs = new FileStream(faceFile, FileMode.Create, FileAccess.Write, FileShare.None);
await (await client.GetStreamAsync(faceUrl).ConfigureAwait(false)).CopyToAsync(faceFs).ConfigureAwait(false);
face = new Uri(faceFile);
}
if (!string.IsNullOrWhiteSpace(coverUrl))
{
using var coverFs = new FileStream(coverFile, FileMode.Create, FileAccess.Write, FileShare.None);
await (await client.GetStreamAsync(coverUrl).ConfigureAwait(false)).CopyToAsync(coverFs).ConfigureAwait(false);
cover = new Uri(coverFile);
}
}
catch (Exception)
{ }
var roomUrl = new Uri("https://live.bilibili.com/" + room.RoomConfig.RoomId);
var builder = new ToastContentBuilder()
.AddHeader("BililiveRecorder-StreamStarted", "B站录播姬开播通知", "")
.AddText(room.Name + " 开播了")
.AddText(room.Title)
.AddText($"{room.AreaNameParent} · {room.AreaNameChild}")
.SetProtocolActivation(roomUrl)
.SetToastDuration(ToastDuration.Long)
.AddButton(new ToastButton().SetContent("打开直播间").SetProtocolActivation(roomUrl))
;
if (time.HasValue)
builder.AddCustomTimeStamp(time.Value);
if (face is not null)
builder.AddAppLogoOverride(face, ToastGenericAppLogoCrop.Circle);
if (cover is not null)
builder.AddInlineImage(cover);
builder.Show();
}
}
}