Android Notification

This commit is contained in:
Aigio Liu
2021-08-05 18:21:51 +08:00
parent edb2f8affb
commit 1a6438eff6
9 changed files with 57 additions and 27 deletions
@@ -199,11 +199,9 @@ namespace System.Application.Services.Implementation
TNotificationType notificationType,
bool autoCancel,
string? title,
TEntrance entrance)
TEntrance? entrance)
{
var notificationEntrance = entrance != null ?
GetActivityType(entrance).GetJClass() :
R.activities.entrance;
var notificationEntrance = (entrance != null ? GetActivityType(entrance)?.GetJClass() : null) ?? R.activities.entrance;
var context = AndroidApplication.Context;
var manager = NotificationManagerCompat.From(context);
var builder = BuildNotify(context, manager, text, notificationType,
@@ -213,24 +211,25 @@ namespace System.Application.Services.Implementation
}
public Progress<float> NotifyDownload(
string text,
Func<string> text,
TNotificationType notificationType,
string? title)
{
var context = AndroidApplication.Context;
var manager = NotificationManagerCompat.From(context);
var builder = BuildNotify(context, manager,
text: text.Format(0),
text: text(),
notificationType,
title: title);
// 进度单位说明
// 通用层采用 float 浮点数作为进度值,范围从0~100,保留两位小数
// 平台层采用 int 整型作为进度值,范围从0~10000,转换需要 乘 100
const int unit_convert_multiple = 100; // 从float到int转换单位倍数
const int PROGRESS_MAX = 100 * unit_convert_multiple; // 进度条满值
const float PROGRESS_MAX = IAppUpdateService.MaxProgressValue * unit_convert_multiple; // 进度条满值
int PROGRESS_MAX_INT32 = PROGRESS_MAX.ToInt32();
// 发出零进度的初始通知
// Issue the initial notification with zero progress
builder.SetProgress(PROGRESS_MAX, 0, false);
builder.SetProgress(PROGRESS_MAX_INT32, 0, false);
var notifyId = GetNotifyId(notificationType);
manager.Notify(notifyId, builder.Build());
// 在这里完成跟踪进度的工作。
@@ -248,7 +247,7 @@ namespace System.Application.Services.Implementation
void Handler(float current)
{
var currentInt32 = (current * unit_convert_multiple).ToInt32();
if (currentInt32 >= PROGRESS_MAX)
if (currentInt32 >= PROGRESS_MAX_INT32)
{
// 这将使100%时直接取消通知,并不会在UI上显示
// 报告进度值满的操作应当是幂等的
@@ -266,8 +265,8 @@ namespace System.Application.Services.Implementation
// 在报告进度值满后不可再更改进度
if (builder == null) throw new ArgumentNullException(nameof(builder));
if (manager == null) throw new ArgumentNullException(nameof(manager));
builder.SetProgress(PROGRESS_MAX, currentInt32, false);
builder.SetContentText(text.Format(currentInt32));
builder.SetProgress(PROGRESS_MAX_INT32, currentInt32, false);
builder.SetContentText(text());
manager.Notify(notifyId, builder.Build());
}
}
@@ -45,7 +45,7 @@ namespace System.Application.Services
/// 下载进度通知
/// </summary>
Progress<float> NotifyDownload(
string text,
Func<string> text,
TNotificationType notificationType,
string? title = default);
}
@@ -2,7 +2,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
mc:Ignorable="d"
d:DesignWidth="720" d:DesignHeight="400"
MinWidth="720" MinHeight="400"
Width="720" Height="400"
x:Class="System.Application.UI.Views.Windows.AddAuthWindow"
xmlns:resx="clr-namespace:System.Application.UI.Resx;assembly=System.Application.SteamTools.Client.Desktop"
xmlns:vm="clr-namespace:System.Application.UI.ViewModels;assembly=System.Application.SteamTools.Client.Desktop"
@@ -10,8 +13,6 @@
xmlns:controls="clr-namespace:System.Application.UI.Views.Controls"
xmlns:ms="clr-namespace:System.Application.Models.Settings;assembly=System.Application.SteamTools.Client.Desktop"
FontFamily="{Binding Source={x:Static ms:UISettings.FontName},Path=Value, Mode=OneWay,Converter={StaticResource NameToFontFamilyConverter}}"
MinWidth="600"
MinHeight="400"
WindowStartupLocation="CenterOwner"
ExtendClientAreaTitleBarHeightHint="30"
Background="Transparent"
@@ -6,13 +6,16 @@ using System.Application.Models;
using System.Windows;
using XEPlatform = Xamarin.Essentials.Platform;
using static System.Application.UI.Resx.AppResources;
using static System.Application.Services.IAppUpdateService;
namespace System.Application.Services.Implementation
{
internal sealed partial class MobileAppUpdateServiceImpl : AppUpdateServiceImpl
{
public MobileAppUpdateServiceImpl(IToast toast, ICloudServiceClient client, IOptions<AppSettings> options) : base(toast, client, options)
readonly INotificationService notification;
public MobileAppUpdateServiceImpl(INotificationService notification, IToast toast, ICloudServiceClient client, IOptions<AppSettings> options) : base(toast, client, options)
{
this.notification = notification;
}
protected override Version OSVersion =>
@@ -46,5 +49,26 @@ namespace System.Application.Services.Implementation
throw new NotImplementedException();
#endif
}
IProgress<float>? progress;
protected override void OnReport(float value, string str)
{
base.OnReport(value, str);
if (value == 0)
{
progress?.Report(MaxProgressValue);
progress = notification.NotifyDownload(() => ProgressString,
NotificationType.NewVersionDownloadProgress);
}
else if (value == MaxProgressValue)
{
progress?.Report(MaxProgressValue);
progress = null;
}
else
{
progress?.Report(value);
}
}
}
}
@@ -13,6 +13,8 @@ using XEPlatform = Xamarin.Essentials.Platform;
using XEVersionTracking = Xamarin.Essentials.VersionTracking;
using System.Application.Models.Settings;
using AndroidX.AppCompat.App;
using System.Application.Services;
using System.Application.Services.Implementation;
#if DEBUG
using Square.LeakCanary;
#endif
@@ -79,6 +81,10 @@ namespace System.Application.UI
Startup.Init(level);
if (IsMainProcess)
{
if (INotificationService.Instance is PlatformNotificationServiceImpl notification)
{
notification.InitNotificationChannels(this);
}
XEVersionTracking.Track();
ImageLoader.Init(this);
UISettings.Theme.Subscribe(x =>
@@ -1,4 +1,4 @@
using System.Application.Properties;
using System.Application.Properties;
using System.Application.Services;
namespace System.Application
+7 -7
View File
@@ -23,16 +23,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="QRCoder" Version="1.4.1"/>
<PackageReference Include="QRCoder" Version="1.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\references\ArchiSteamFarm\ArchiSteamFarm.Library\ArchiSteamFarm.Library.csproj"/>
<ProjectReference Include="..\..\references\Titanium-Web-Proxy\src\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj"/>
<ProjectReference Include="..\Repositories.sqlite-net-pcl\Repositories.sqlite-net-pcl.csproj"/>
<ProjectReference Include="..\ST.Services.CloudService\ST.Services.CloudService.csproj"/>
<ProjectReference Include="..\Common.ClientLib\Common.ClientLib.csproj"/>
<ProjectReference Include="..\ST\ST.csproj"/>
<ProjectReference Include="..\..\references\ArchiSteamFarm\ArchiSteamFarm.Library\ArchiSteamFarm.Library.csproj" />
<ProjectReference Include="..\..\references\Titanium-Web-Proxy\src\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj" />
<ProjectReference Include="..\Repositories.sqlite-net-pcl\Repositories.sqlite-net-pcl.csproj" />
<ProjectReference Include="..\ST.Services.CloudService\ST.Services.CloudService.csproj" />
<ProjectReference Include="..\Common.ClientLib\Common.ClientLib.csproj" />
<ProjectReference Include="..\ST\ST.csproj" />
</ItemGroup>
<ItemGroup>
@@ -211,7 +211,7 @@ namespace System.Application.Services.Implementation
protected void OnReportCalcHashing(float value) => OnReport(value, SR.CalcHashing_.Format(MathF.Round(value, 2)));
protected void OnReportDecompressing(float value) => OnReport(value, SR.Decompressing_.Format(MathF.Round(value, 2)));
protected void OnReport(float value = 0f) => OnReport(value, string.Empty);
protected void OnReport(float value, string str)
protected virtual void OnReport(float value, string str)
{
ProgressValue = value;
ProgressString = str;
+2 -2
View File
@@ -1,4 +1,4 @@
namespace System.Application
namespace System.Application
{
/// <summary>
/// 通知类型
@@ -17,7 +17,7 @@
/// </item>
/// </list>
/// <para>添加新的枚举常量注意事项:</para>
/// <para>(客户端)如果要显示在设备的通知栏上,则需要在<see cref="NotificationType_Channel_EnumExtensions.GetChannel(NotificationType)"/>中添加对应渠道</para>
/// <para>(客户端)如果要显示在设备的通知栏上,则需要在 src\ST.Client\Extensions\NotificationType_Channel_EnumExtensions.cs 中添加或指定已有渠道</para>
/// </summary>
public enum NotificationType
{