diff --git a/src/WorkFlowCheck.MAUI/App.xaml.cs b/src/WorkFlowCheck.MAUI/App.xaml.cs index dd2bd17..df91181 100644 --- a/src/WorkFlowCheck.MAUI/App.xaml.cs +++ b/src/WorkFlowCheck.MAUI/App.xaml.cs @@ -8,7 +8,15 @@ using WorkFlowCheck.MAUI.DataLayer; using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Services; using WorkFlowCheck.MAUI.Services.Interfaces; +using Android.Content.PM; +using WorkFlowCheck.MAUI.Platforms.Android.Services; + + +#if ANDROID +using Android.Content; +using WorkFlowCheck.MAUI.Platforms.Android; +#endif namespace WorkFlowCheck.MAUI { public partial class App : Application @@ -29,14 +37,15 @@ namespace WorkFlowCheck.MAUI _dbContext.Database.EnsureDeleted(); // Adatbázis törlése _dbContext.Database.EnsureCreated(); // Új adatbázis létrehozása - StartBackgroundTask(); + + //StartBackgroundTask(); MainPage = new AppShell(); //MainPage = new NavigationPage(new MainPage(serviceProvider.GetRequiredService())); } protected async override void OnResume() { - StartBackgroundTask(); + //StartBackgroundTask(); base.OnResume(); var isLogged = await SecureStorage.Default.GetAsync("WFCUser"); if (isLogged == null) diff --git a/src/WorkFlowCheck.MAUI/Helper/ServiceHelper.cs b/src/WorkFlowCheck.MAUI/Helper/ServiceHelper.cs new file mode 100644 index 0000000..3bf9ab9 --- /dev/null +++ b/src/WorkFlowCheck.MAUI/Helper/ServiceHelper.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WorkFlowCheck.MAUI.Helper +{ + public static class ServiceHelper + { + public static IServiceProvider Services { get; set; } + } +} diff --git a/src/WorkFlowCheck.MAUI/MainPage.xaml.cs b/src/WorkFlowCheck.MAUI/MainPage.xaml.cs index 9ad3656..255116f 100644 --- a/src/WorkFlowCheck.MAUI/MainPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/MainPage.xaml.cs @@ -1,7 +1,9 @@ -using DevExpress.Entity.Model.Metadata; +using Android.Content; +using DevExpress.Entity.Model.Metadata; using WorkFlowCheck.Common.DTO; using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Pages.Media; +using WorkFlowCheck.MAUI.Platforms.Android.Services; using WorkFlowCheck.MAUI.Services.Interfaces; namespace WorkFlowCheck.MAUI { @@ -15,6 +17,7 @@ namespace WorkFlowCheck.MAUI { InitializeComponent(); _userService = userService; + } private void SetInfo() diff --git a/src/WorkFlowCheck.MAUI/MauiProgram.cs b/src/WorkFlowCheck.MAUI/MauiProgram.cs index 87c562d..73171d0 100644 --- a/src/WorkFlowCheck.MAUI/MauiProgram.cs +++ b/src/WorkFlowCheck.MAUI/MauiProgram.cs @@ -65,6 +65,7 @@ namespace WorkFlowCheck.MAUI ServiceProvider = app.Services; + ServiceHelper.Services = app.Services; return app; } diff --git a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs index ad62fba..128e944 100644 --- a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs @@ -1,8 +1,10 @@ +using Android.Content; using CommunityToolkit.Maui.Alerts; using CommunityToolkit.Maui.Core; using System.Drawing; using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Pages.NFC; +using WorkFlowCheck.MAUI.Platforms.Android.Services; using WorkFlowCheck.MAUI.Services.Interfaces; namespace WorkFlowCheck.MAUI.Pages.Security; @@ -33,9 +35,13 @@ public partial class LoginPage : BasePage if (response != null && response.IsSuccess) { _userService.SetCurrentUser(response.Data); - SystemHelper.SystemUserDTO = response.Data; + SystemHelper.SystemUserDTO = response.Data; + +#if ANDROID + var intent = new Intent(Android.App.Application.Context, typeof(NotifyService)); + Android.App.Application.Context.StartForegroundService(intent); +#endif - var wFCUser = Newtonsoft.Json.JsonConvert.SerializeObject(response.Data); await SecureStorage.Default.SetAsync("WFCUser", "wFCUser"); diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml b/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml index 1e83531..f1ba3a8 100644 --- a/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml @@ -15,6 +15,11 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> + @@ -23,5 +28,7 @@ + + \ No newline at end of file diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/drawable/notification_icon.png b/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/drawable/notification_icon.png new file mode 100644 index 0000000..2144fb2 Binary files /dev/null and b/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/drawable/notification_icon.png differ diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/Services/NotifyService.cs b/src/WorkFlowCheck.MAUI/Platforms/Android/Services/NotifyService.cs new file mode 100644 index 0000000..28991f0 --- /dev/null +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/Services/NotifyService.cs @@ -0,0 +1,124 @@ +using Android.App; +using Android.Content; +using Android.OS; +using System.Threading; +using System.Threading.Tasks; +using WorkFlowCheck.MAUI.Helper; +using WorkFlowCheck.MAUI.Services.Interfaces; + +namespace WorkFlowCheck.MAUI.Platforms.Android.Services +{ + [Service] + public class NotifyService : Service + { + private CancellationTokenSource _cts; + private IUserService _userService; + private ISyncService _syncService; + public NotifyService() + { + + } + public override IBinder OnBind(Intent intent) => null; + + public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) + { + CreateNotificationChannel(); + + var notification = new Notification.Builder(this, "polling_channel") + .SetContentTitle("Értesítés figyelés") + .SetContentText("A háttérfigyelés aktív") + .SetSmallIcon(Resource.Drawable.notification_icon) + .SetVisibility(NotificationVisibility.Public) // Lock screen-en is látszódjon + .SetPriority((int)NotificationPriority.High) + .Build(); + + StartForeground(1, notification); + + _cts = new CancellationTokenSource(); + + var _syncService = ServiceHelper.Services.GetService(typeof(ISyncService)) as ISyncService; + var _userService = ServiceHelper.Services.GetService(typeof(IUserService)) as IUserService; + Task.Run(async () => + { + await _syncService.SyncUsers_Down((progress, message) => + { + + }); + + }, _cts.Token); + var userDTO = _userService.GetCurrentUser(); + if (userDTO != null && userDTO.Id > 0) + { + if (userDTO.RoleDTO?.Count > 0) + { + foreach (var roleDTO in userDTO.RoleDTO) + { + if (roleDTO.CanEnableBlocked) + { + + + Task.Run(async () => + { + + + while (!_cts.Token.IsCancellationRequested) + { + var deviceMessageDTOList = await _syncService.DeviceMessageReadUnreaded(_userService.DeviceId.ToUpper()); + if (deviceMessageDTOList != null && deviceMessageDTOList.Count > 0) + { + ShowNotification("Új esemény", deviceMessageDTOList[0].Message); + break; + } + deviceMessageDTOList = await _syncService.DeviceMessageReadUnreadedRole(roleDTO.Id); + if (deviceMessageDTOList != null && deviceMessageDTOList.Count > 0) + { + ShowNotification("Új esemény", deviceMessageDTOList[0].Message); + break; + } + await Task.Delay(5000); + } + }, _cts.Token); + } + } + } + } + return StartCommandResult.Sticky; + } + + + public override void OnDestroy() + { + _cts?.Cancel(); + base.OnDestroy(); + } + + private void CreateNotificationChannel() + { + if (Build.VERSION.SdkInt >= BuildVersionCodes.O) + { + var channel = new NotificationChannel("polling_channel", "Polling Channel", NotificationImportance.High) + { + Description = "Foreground service polling channel", + LockscreenVisibility = NotificationVisibility.Public + }; + + var notificationManager = (NotificationManager)GetSystemService(NotificationService); + notificationManager.CreateNotificationChannel(channel); + } + } + + private void ShowNotification(string title, string message) + { + var builder = new Notification.Builder(this, "polling_channel") + .SetContentTitle(title) + .SetContentText(message) + .SetSmallIcon(Resource.Drawable.notification_icon) + .SetAutoCancel(true) + .SetVisibility(NotificationVisibility.Public) + .SetPriority((int)NotificationPriority.High); + + var notificationManager = (NotificationManager)GetSystemService(NotificationService); + notificationManager.Notify(new System.Random().Next(), builder.Build()); + } + } +} diff --git a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj index 12d028a..01f62ad 100644 --- a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj +++ b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj @@ -31,7 +31,7 @@ - 21.0 + 29.0 @@ -159,4 +159,10 @@ + + + Never + + +