Divided the code into functional modules
This commit is contained in:
72
Services/Platform/AndroidNotificationService.cs
Normal file
72
Services/Platform/AndroidNotificationService.cs
Normal file
@ -0,0 +1,72 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Android平台通知服务实现
|
||||
/// </summary>
|
||||
public class AndroidNotificationService : INotificationService
|
||||
{
|
||||
private const string CHANNEL_ID = "HeartRateMonitorChannel";
|
||||
private const int NOTIFICATION_ID = 100;
|
||||
private const int RECONNECTION_NOTIFICATION_ID = 101;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CreateNotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"心率监测",
|
||||
"显示实时心率数据");
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
string bigText = $"当前心率: {currentHeartRate} bpm\n监测时长: {duration.Hours:00}:{duration.Minutes:00}:{duration.Seconds:00}\n最低: {minHeartRate} bpm | 最高: {maxHeartRate} bpm";
|
||||
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowBigTextNotification(
|
||||
CHANNEL_ID,
|
||||
NOTIFICATION_ID,
|
||||
title,
|
||||
content,
|
||||
bigText,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
true);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CancelNotification(NOTIFICATION_ID);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowNormalNotification(
|
||||
CHANNEL_ID,
|
||||
RECONNECTION_NOTIFICATION_ID,
|
||||
title,
|
||||
message,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
false); // 不使用前台服务,只显示普通通知
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Services/Platform/IosNotificationService.cs
Normal file
52
Services/Platform/IosNotificationService.cs
Normal file
@ -0,0 +1,52 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// iOS平台通知服务实现
|
||||
/// </summary>
|
||||
public class IosNotificationService : INotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
#if IOS
|
||||
// 请求iOS通知权限
|
||||
Platforms.iOS.IosNotificationHelper.RequestNotificationPermission().ConfigureAwait(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.CancelAllNotifications();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
49
Services/Platform/WindowsNotificationService.cs
Normal file
49
Services/Platform/WindowsNotificationService.cs
Normal file
@ -0,0 +1,49 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows平台通知服务实现
|
||||
/// </summary>
|
||||
public class WindowsNotificationService : INotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
// Windows平台不需要特殊初始化
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.CancelNotification();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user