使用 Firebase 云消息传递的 .NET MAUI 推送通知

Mat*_*vic 3 .net c# maui

我想使用 Firebase Cloud Messaging 实现一个非常简单的推送通知系统。

MAUI 项目构建在 .NET 8 版本之上。

我已按照指南来设置项目。

指南使用 .net6 的地方我替换为 .net8

所以我的 .csproj 文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net8.0-android;net8.0-ios;</TargetFrameworks>
        <OutputType>Exe</OutputType>
        <RootNamespace>PushNotificationsTest</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>

        <!-- Display name -->
        <ApplicationTitle>PushNotificationsTest</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.pushnotificationstest</ApplicationId>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>

    <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
        <GoogleServicesJson Include="google-services.json" />
    </ItemGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0-rc.1.23419.4" />
        <PackageReference Include="Plugin.Firebase" Version="2.0.7" />
        <PackageReference Include="Plugin.FirebaseCrashlytics" Version="4.1.0" />
    </ItemGroup>

    <!-- Build Properties must be defined within these property groups to ensure successful publishing
       to the Mac App Store. See: https://aka.ms/maui-publish-app-store#define-build-properties-in-your-project-file -->
    <PropertyGroup Condition="$(TargetFramework.Contains('-maccatalyst')) and '$(Configuration)' == 'Debug'">
        <CodesignEntitlements>Platforms/MacCatalyst/Entitlements.Debug.plist</CodesignEntitlements>
    </PropertyGroup>

    <PropertyGroup Condition="$(TargetFramework.Contains('-maccatalyst')) and '$(Configuration)' == 'Release'">
        <CodesignEntitlements>Platforms/MacCatalyst/Entitlements.Release.plist</CodesignEntitlements>
        <UseHardenedRuntime>true</UseHardenedRuntime>
    </PropertyGroup>
</Project>

Run Code Online (Sandbox Code Playgroud)

但是当我想运行该应用程序时,出现以下异常:

Error JAVA0000 java.exe: Error in C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.jvm\1.3.0.1\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-jvm.jar:androidx/collection/ArraySetKt.class:
Type androidx.collection.ArraySetKt is defined multiple times: C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.jvm\1.3.0.1\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-jvm.jar:androidx/collection/ArraySetKt.class, C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.ktx\1.2.0.9\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-ktx.jar:androidx/collection/ArraySetKt.class
Compilation failed
java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.jvm\1.3.0.1\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-jvm.jar
androidx/collection/ArraySetKt.class
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:126)
    at com.android.tools.r8.D8.main(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:5)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.jvm\1.3.0.1\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-jvm.jar:androidx/collection/ArraySetKt.class
    at Version.fakeStackEntry(Version_8.1.56.java:0)
    at com.android.tools.r8.M.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:5)
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:81)
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:32)
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:31)
    at com.android.tools.r8.utils.R0.b(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:2)
    at com.android.tools.r8.D8.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:26)
    at com.android.tools.r8.D8.b(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:13)
    at com.android.tools.r8.D8.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:24)
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:113)
    ... 1 more
Caused by: com.android.tools.r8.utils.b: Type androidx.collection.ArraySetKt is defined multiple times: C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.jvm\1.3.0.1\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-jvm.jar:androidx/collection/ArraySetKt.class, C:\Users\mdavidovic\.nuget\packages\xamarin.androidx.collection.ktx\1.2.0.9\buildTransitive\net6.0-android31.0\..\..\jar\androidx.collection.collection-ktx.jar:androidx/collection/ArraySetKt.class
    at com.android.tools.r8.utils.O2.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:21)
    at com.android.tools.r8.utils.O2.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:26)
    at com.android.tools.r8.utils.A2.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:44)
    at com.android.tools.r8.utils.A2.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:10)
    at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2048)
    at com.android.tools.r8.utils.A2.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:6)
    at com.android.tools.r8.graph.b4$a.e(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:7)
    at com.android.tools.r8.dex.c.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:58)
    at com.android.tools.r8.dex.c.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:9)
    at com.android.tools.r8.dex.c.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:8)
    at com.android.tools.r8.D8.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:29)
    at com.android.tools.r8.D8.d(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:17)
    at com.android.tools.r8.D8.c(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:1)
    at com.android.tools.r8.utils.R0.a(R8_8.1.56_756d1f50f618dd1c39c000f11defb367a21e9e866e3401b884be16c0950f6f79:28)
    ... 6 more

0>------- Finished building project: PushNotificationsTest. Succeeded: False. Errors: 1. Warnings: 3
Run Code Online (Sandbox Code Playgroud)

我一直在寻找解决方案,但我还没有看到有人将 MAUI 与 .NET 8 一起使用来推送通知。

有人可以帮助我吗?

感谢转发!

我从 .csproj 文件中删除了这段代码,因为无论有没有它,错误都是相同的:

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">
    <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="1.7.10" ExcludeAssets="build;buildTransitive" />
    <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="1.7.10" ExcludeAssets="build;buildTransitive" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

我尝试删除 bin 和 obj 文件夹并清理整个解决方案,但似乎没有任何帮助。

我也尝试按照@moljac的答案安装“Xamarin.AndroidX.Fragment.Ktx”nuget包,但错误是相同的......

jas*_*ith 7

几天前我就遇到了这个问题,尽管.net 版本不同。

我试图回忆一下我是如何解决这个问题的,我认为是通过添加这两个包引用来解决的;

<PackageReference Include="Xamarin.AndroidX.Preference" Version="1.2.1.2" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.10.0.1" />
Run Code Online (Sandbox Code Playgroud)

您可能还需要在重建之前进行清洁。

  • 谢谢大佬,这个问题已经解决了 最主要的是,我必须将它们都放入 GoogleServiceJson 所在的 ItemGroup 中,否则它会抱怨版本说即使在清理和重建之后,这两个包也不兼容 .NET 8。 (2认同)