diff --git a/src/WorkFlowCheck.MAUI/MauiProgram.cs b/src/WorkFlowCheck.MAUI/MauiProgram.cs index 87c562d..43a7989 100644 --- a/src/WorkFlowCheck.MAUI/MauiProgram.cs +++ b/src/WorkFlowCheck.MAUI/MauiProgram.cs @@ -12,7 +12,12 @@ using Microsoft.Maui.Hosting; using CommunityToolkit.Maui.Core; using WorkFlowCheck.MAUI.Handlers; using WorkFlowCheck.MAUI.Helper; +using Plugin.Firebase.CloudMessaging; +using Microsoft.Maui.LifecycleEvents; +#if ANDROID +using Plugin.Firebase.Core.Platforms.Android; +#endif namespace WorkFlowCheck.MAUI { public static class MauiProgram @@ -23,7 +28,7 @@ namespace WorkFlowCheck.MAUI builder.UseDevExpress(useLocalization: false) .UseDevExpressControls() .UseDevExpressCollectionView(); - + builder.Services.AddAutoMapper(typeof(MapperProfile)); builder.Services.AddHttpClient(); builder.Services.AddDbContext(); @@ -46,7 +51,7 @@ namespace WorkFlowCheck.MAUI fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); fonts.AddFont("FontAwesome.ttf", "FontAwesome"); }) - + .RegisterFirebaseServices() .UseMauiCommunityToolkitCore() .UseMauiCommunityToolkit(); #if DEBUG @@ -63,11 +68,24 @@ namespace WorkFlowCheck.MAUI }).GetAwaiter().GetResult(); } - ServiceProvider = app.Services; return app; } public static IServiceProvider ServiceProvider { get; private set; } + + + + private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder) + { + builder.ConfigureLifecycleEvents(events => + { +#if ANDROID + events.AddAndroid(android => android.OnCreate((activity, _) => + CrossFirebase.Initialize(activity))); +#endif + }); + return builder; + } } } \ No newline at end of file diff --git a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs index ad62fba..fce8fad 100644 --- a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Maui.Alerts; using CommunityToolkit.Maui.Core; +using Plugin.Firebase.CloudMessaging; using System.Drawing; using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Pages.NFC; @@ -14,6 +15,7 @@ public partial class LoginPage : BasePage public LoginPage() { InitializeComponent(); + InitFirebase(); SetContent(LoginContent); _userService = MauiProgram.ServiceProvider.GetRequiredService(); _syncService = MauiProgram.ServiceProvider.GetRequiredService(); @@ -166,4 +168,18 @@ public partial class LoginPage : BasePage } } + + private async void InitFirebase() + { + try + { + await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync(); + var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync(); + Console.WriteLine($"FCM token: {token}"); + } + catch (Exception ex) + { + Console.WriteLine("FCM init error: " + ex.Message); + } + } } \ No newline at end of file diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml b/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml index 1e83531..3a0dcc9 100644 --- a/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/AndroidManifest.xml @@ -15,6 +15,18 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> + + + + + + + + @@ -23,5 +35,8 @@ + + + \ No newline at end of file diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs index 30b3b82..f27e69c 100644 --- a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs @@ -3,6 +3,8 @@ using Android.Content; using Android.Content.PM; using Android.Nfc; using Android.OS; +using Firebase; +using Plugin.Firebase.CloudMessaging; using Plugin.NFC; @@ -40,12 +42,14 @@ namespace WorkFlowCheck.MAUI new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.SingleTop), Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : PendingIntentFlags.UpdateCurrent); + HandleIntent(Intent); + CreateNotificationChannelIfNeeded(); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); - + HandleIntent(intent); CrossNFC.OnNewIntent(intent); if (intent.Action == NfcAdapter.ActionTagDiscovered) @@ -85,5 +89,24 @@ namespace WorkFlowCheck.MAUI // nfcAdapter?.DisableForegroundDispatch(this); //} } + private static void HandleIntent(Intent intent) + { + FirebaseCloudMessagingImplementation.OnNewIntent(intent); + } + private void CreateNotificationChannelIfNeeded() + { + if (Build.VERSION.SdkInt >= BuildVersionCodes.O) + { + CreateNotificationChannel(); + } + } + private void CreateNotificationChannel() + { + var channelId = $"{PackageName}.general"; + var notificationManager = (NotificationManager)GetSystemService(NotificationService); + var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default); + notificationManager.CreateNotificationChannel(channel); + FirebaseCloudMessagingImplementation.ChannelId = channelId; + } } } diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/values/strings.xml b/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/values/strings.xml new file mode 100644 index 0000000..795a0ba --- /dev/null +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/Resources/values/strings.xml @@ -0,0 +1,4 @@ + + + none + \ No newline at end of file diff --git a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj index 7bdaef2..c98622b 100644 --- a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj +++ b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj @@ -36,7 +36,9 @@ 10.0.17763.0 6.5--> - + + + @@ -54,6 +56,9 @@ + + +