Firebase teszthez

This commit is contained in:
2025-05-15 09:28:08 +02:00
parent 7efd090b86
commit 9df2854a3c
6 changed files with 86 additions and 5 deletions
+21 -3
View File
@@ -12,7 +12,12 @@ using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui.Core; using CommunityToolkit.Maui.Core;
using WorkFlowCheck.MAUI.Handlers; using WorkFlowCheck.MAUI.Handlers;
using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Helper;
using Plugin.Firebase.CloudMessaging;
using Microsoft.Maui.LifecycleEvents;
#if ANDROID
using Plugin.Firebase.Core.Platforms.Android;
#endif
namespace WorkFlowCheck.MAUI namespace WorkFlowCheck.MAUI
{ {
public static class MauiProgram public static class MauiProgram
@@ -23,7 +28,7 @@ namespace WorkFlowCheck.MAUI
builder.UseDevExpress(useLocalization: false) builder.UseDevExpress(useLocalization: false)
.UseDevExpressControls() .UseDevExpressControls()
.UseDevExpressCollectionView(); .UseDevExpressCollectionView();
builder.Services.AddAutoMapper(typeof(MapperProfile)); builder.Services.AddAutoMapper(typeof(MapperProfile));
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddDbContext<AppDbContext>(); builder.Services.AddDbContext<AppDbContext>();
@@ -46,7 +51,7 @@ namespace WorkFlowCheck.MAUI
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("FontAwesome.ttf", "FontAwesome"); fonts.AddFont("FontAwesome.ttf", "FontAwesome");
}) })
.RegisterFirebaseServices()
.UseMauiCommunityToolkitCore() .UseMauiCommunityToolkitCore()
.UseMauiCommunityToolkit(); .UseMauiCommunityToolkit();
#if DEBUG #if DEBUG
@@ -63,11 +68,24 @@ namespace WorkFlowCheck.MAUI
}).GetAwaiter().GetResult(); }).GetAwaiter().GetResult();
} }
ServiceProvider = app.Services; ServiceProvider = app.Services;
return app; return app;
} }
public static IServiceProvider ServiceProvider { get; private set; } 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;
}
} }
} }
@@ -1,5 +1,6 @@
using CommunityToolkit.Maui.Alerts; using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core; using CommunityToolkit.Maui.Core;
using Plugin.Firebase.CloudMessaging;
using System.Drawing; using System.Drawing;
using WorkFlowCheck.MAUI.Helper; using WorkFlowCheck.MAUI.Helper;
using WorkFlowCheck.MAUI.Pages.NFC; using WorkFlowCheck.MAUI.Pages.NFC;
@@ -14,6 +15,7 @@ public partial class LoginPage : BasePage
public LoginPage() public LoginPage()
{ {
InitializeComponent(); InitializeComponent();
InitFirebase();
SetContent(LoginContent); SetContent(LoginContent);
_userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>(); _userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
_syncService = MauiProgram.ServiceProvider.GetRequiredService<ISyncService>(); _syncService = MauiProgram.ServiceProvider.GetRequiredService<ISyncService>();
@@ -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);
}
}
} }
@@ -15,6 +15,18 @@
android:name="android.support.FILE_PROVIDER_PATHS" android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" /> android:resource="@xml/file_paths" />
</provider> </provider>
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="default" />
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
</application> </application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
@@ -23,5 +35,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-feature android:name="android.hardware.nfc" android:required="true" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest> </manifest>
@@ -3,6 +3,8 @@ using Android.Content;
using Android.Content.PM; using Android.Content.PM;
using Android.Nfc; using Android.Nfc;
using Android.OS; using Android.OS;
using Firebase;
using Plugin.Firebase.CloudMessaging;
using Plugin.NFC; using Plugin.NFC;
@@ -40,12 +42,14 @@ namespace WorkFlowCheck.MAUI
new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.SingleTop), new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.SingleTop),
Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : PendingIntentFlags.UpdateCurrent); Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : PendingIntentFlags.UpdateCurrent);
HandleIntent(Intent);
CreateNotificationChannelIfNeeded();
} }
protected override void OnNewIntent(Intent intent) protected override void OnNewIntent(Intent intent)
{ {
base.OnNewIntent(intent); base.OnNewIntent(intent);
HandleIntent(intent);
CrossNFC.OnNewIntent(intent); CrossNFC.OnNewIntent(intent);
if (intent.Action == NfcAdapter.ActionTagDiscovered) if (intent.Action == NfcAdapter.ActionTagDiscovered)
@@ -85,5 +89,24 @@ namespace WorkFlowCheck.MAUI
// nfcAdapter?.DisableForegroundDispatch(this); // 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;
}
} }
} }
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<string name="com.google.firebase.crashlytics.mapping_file_id">none</string>
</resources>
@@ -36,7 +36,9 @@
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>--> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>-->
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<GoogleServicesJson Include="Platforms\Android\google-services.json" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<!-- App Icon --> <!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
@@ -54,6 +56,9 @@
<!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<AndroidResource Remove="Platforms\Android\Resources\values\strings.xml" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<AndroidResource Include="Platforms\Android\Resources\xml\file_paths.xml" /> <AndroidResource Include="Platforms\Android\Resources\xml\file_paths.xml" />