TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Does this work with .NET MAUI Blazor-App? #96

Closed ragininternet closed 3 months ago

ragininternet commented 1 year ago

Does this work with .NET MAUI Blazor-App? Or is this only for the Xamarin .NET MAUI-App?

I used it in an MAUI BLazor-App and an Error happend as I installed the Nugetpacket via manager.

.... System.IO.DirectoryNotFoundException: Ein Teil des Pfades "C:\Users\Are.nuget\packages\xamarin.firebase.ios.installations\8.10.0.1\lib\net6.0-ios15.4\Firebase.Installations.resources\FirebaseInstallations.xcframework\ios-arm64_i386_x86_64-simulator\FirebaseInstallations.framework\Headers\FirebaseInstallations-umbrella.h" konnte nicht gefunden werden. bei System.IO.Error.WinIOError(Int32 errorCode, String maybeFullPath) bei System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) bei System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) bei System.IO.File.Create(String path) bei NuGet.Packaging.StreamExtensions.Testable.MmapCopy(Stream inputStream, String fileFullPath, Int64 size) bei NuGet.Packaging.StreamExtensions.Testable.CopyToFile(Stream inputStream, String fileFullPath) bei NuGet.Packaging.PackageFileExtractor.ExtractPackageFile(String source, String target, Stream stream) bei NuGet.Packaging.PackageArchiveReader.CopyFiles(String destination, IEnumerable1 packageFiles, ExtractPackageFileDelegate extractFile, ILogger logger, CancellationToken token) bei NuGet.Packaging.PackageReaderBase.CopyFilesAsync(String destination, IEnumerable1 packageFiles, ExtractPackageFileDelegate extractFile, ILogger logger, CancellationToken cancellationToken) bei NuGet.Packaging.PackageExtractor.<>c__DisplayClass5_0.<b0>d.MoveNext() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- ....

TobiasBuchholz commented 1 year ago

Unfortunately I don't have any experience regarding to .NET MAUI Blazor apps, so I'm not sure whether this should work or not. Theoretically it could work since in combination with .NET MAUI is Blazor is just another UI framework solution for creating an android or iOS app as far as I understand it.

Maybe referencing Xamarin.Firebase.iOS.Installations directly in your .csproj file solves your problem? Sorry I can't assist any further but please let us know if you get it to work.

Tornadocraver commented 1 year ago

I am getting the same error, but with a normal .NET 7.0 MAUI app (not Blazor). Installing version 1.2.3 via NuGet fails with the following error:

Could not find a part of the path 'C:\Users\<username>\.nuget\packages\xamarin.firebase.ios.core\8.10.0.1\lib\
xamarinios10\Firebase.Core.resources\GoogleUtilitiesComponents.xcframework\ios-arm64_i386_x86_64-simulator\
GoogleUtilitiesComponents.framework\PrivateHeaders\GULCCComponentContainerInternal.h'.

I tried versions 1.2.2 and 1.2.1, but the error occurs with different iOS header files.

This started happening after I updated Visual Studio 2022 and installed .NET 7.0. I was able to successfully install it on a .NET 6.0 MAUI app prior to the updates, but now neither .NET 6.0 or 7.0 work.

angelru commented 1 year ago

The same issue when trying to upgrade from 1.2.2 to 1.2.3 .NET MAUI (whitout blazor)

AlleSchonWeg commented 1 year ago

This is a nuget problem. Nuget has problems with long directory names. As a workaround create a environment valriable NUGET_PACKAGES, set it's value to C:\nuget and restart Visual Studio. Then you can try again.

Tornadocraver commented 1 year ago

Thanks @AlleSchonWeg - the workaround works for version 1.2.2 and MAUI 7.0 (which is good enough for me). I tried it with version 1.2.3 though, and it still fails with the same error.

binhtt6 commented 1 year ago

Get this project working with blazor maui .NET 7, thanks so much

angelru commented 1 year ago

https://github.com/xamarin/GoogleApisForiOSComponents/issues/555#issuecomment-1145943195

PowerShell: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell

New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

see this: https://github.com/TobiasBuchholz/Plugin.Firebase/issues/65

I was able to update successfully

AlleSchonWeg commented 1 year ago

The path length is limited to 260 chars per default: https://github.com/NuGet/Home/issues/11953

PhilippRoessner commented 1 year ago

It's working now on my Windows 11 machine for Android. I successfully received a fcm-token, YAY! I had to install the package with the terminal tho.

I put the builder.RegisterFirebaseServices() to the end of my service registrations:

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        });

    builder.Services.AddMauiBlazorWebView();
    #if DEBUG
    builder.Services.AddBlazorWebViewDeveloperTools();
#endif

    builder.Services.AddSingleton<WeatherForecastService>();
    builder.RegisterFirebaseServices();

    return builder.Build();
}

When I try to run the app on a iOS Device, I get the following error:

[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: '   at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.RegisterForRemoteNotifications()
   at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.Initialize()
   at Plugin.Firebase.iOS.CrossFirebase.Initialize(UIApplication app, NSDictionary options, CrossFirebaseSettings settings, Options firebaseOptions, String name)
   at MauiAppPush.MauiProgram.<>c.<RegisterFirebaseServices>b__1_3(UIApplication app, NSDictionary launchOptions) in C:\Users\Phili\source\repos\MauiAppPush\MauiAppPush\MauiProgram.cs:line 35
   at Microsoft.Maui.MauiUIApplicationDelegate.<>c__DisplayClass6_0.<FinishedLaunching>b__0(FinishedLaunching del)
   at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[FinishedLaunching](IServiceProvider services, Action`1 action)
   at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at MauiAppPush.Program.Main(String[] args) in C:\Users\Phili\source\repos\MauiAppPush\MauiAppPush\Platforms\iOS\Program.cs:line 13
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)'

I think the state of the app has a different behavior with Blazor Maui - mode.

Any Ideas about iOS?

PhilippRoessner commented 1 year ago

@TobiasBuchholz

The Issue Happens in iOS/Auth/ FirebaseAuthImplementation.cs -> 29

The binding for the GoogleService-INfo.plist seems to fail, Ideas?

TobiasBuchholz commented 1 year ago

Did you put the GoogleService-Info.plist in the root of your project and referenced it in your .csproj file as described in the setup?

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-ios'">
    <BundleResource Include="GoogleService-Info.plist" />
</ItemGroup>

If you are targetting .net7 you also need to change the ItemGroup Condition to '$(TargetFramework)' == 'net7.0-ios'. Maybe that's the problem?

PhilippRoessner commented 1 year ago

I checked that but unfortunately it's not working. I also tried different combinations with putting it inside the iOS folder or changing the Configuration like this:

    <ItemGroup Condition="$(TargetFramework.Contains('net6.0-ios'))">
        <MauiAsset Include="Platforms\iOS\GoogleService-Info.plist" LogicalName="GoogleService-Info.plist" />
    </ItemGroup>

without luck :-(

kirakosyan commented 1 year ago

It is a an issue with Blazor MAUI. I had to put the file out of project folder. like

then it started to build and work without issues.

While doing it in normal MAUI, without Blazor, it will work in the root folder.
foppenm commented 1 year ago

It is a an issue with Blazor MAUI. I had to put the file out of project folder. like

then it started to build and work without issues.

While doing it in normal MAUI, without Blazor, it will work in the root folder.

Can you explain in some more detail about this? Still a struggle to get it working. For now it seems that Blazor only accepts the files in the wwwroot folder. By putting the google-services.json and the GoogleService-info.plist in there and referencing it through a MauiAsset like below.

 <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
      <MauiAsset Include="wwwroot\MyFolder\google-services.json" LogicalName="google-services.json" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-ios'">
      <MauiAsset Include="wwwroot\MyFolder\GoogleService-info.plist" LogicalName="GoogleService-info.plist" />
  </ItemGroup>

The original error that started this was: The path '../../../../../../../../../Users/<user>/RiderProjects/MauiApp2/MauiApp2/google-services.json' would result in a file outside of the app bundle and cannot be used.

angelru commented 1 year ago

Did they get it to work?

harima34 commented 1 year ago

@PhilippRoessner Hi there, i also have this problem on .net maui blazor ios, working fine with android, but ios i got the same issue "[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImpleme". Did you find a solution ?

AdamEssenmacher commented 8 months ago

Two separate issues in here. One is a duplicate of the long filenames issue: https://github.com/TobiasBuchholz/Plugin.Firebase/issues/240#issuecomment-1889938825

The other is how to use Firebase in a MAUI Blazor app. Yes, it should work. However, putting GoogleService-info.plist/GoogleServices.json in the wwwroot folder is the wrong approach.

To use this plugin in a MAUI Blazor app, you should set it up and initialize it just like a non-Blazor MAUI app, register the firebase services, and then inject them into your Blazor components.