错误 java.exe 退出,代码为 1 Xamarin Firbase Messaging

Eyn*_*ani 0 push-notification xamarin.android firebase xamarin.forms firebase-cloud-messaging

我正在使用 nuget 包Xamarin.Firebase.MessagingXamarin.GooglePlayServices.Base在我的应用程序中接收推送通知,以前它工作正常,但是当我更新时visual studio 2022 to 17.2.3它停止工作

我尝试了所有这些:

  • 更新所有nuget包
  • 从所有共享项目中删除 obj bin 文件夹
  • 启用多重分包
  • 安装并包含

<PackageReference Include="Xamarin.Google.Guava" ExcludeAssets="all"> <Version>27.1.0</Version> </PackageReference>

我之前所做的一切都不起作用

我的接收推送通知的代码:

using System;
using System.Threading.Tasks;
using Android.App;
using Firebase.Messaging;
using Plugin.DeviceInfo;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace MyApp.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]

    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        readonly AndroidNotificationManager _androidNotification = new AndroidNotificationManager();
        public override void OnMessageReceived(RemoteMessage message)
        {
            var mensajeData = message.Data;

            string title= mensajeData["notiTitle"];
            string bodymessage= mensajeData["notiBody"];

            _androidNotification.CreateLocalNotification(title, bodymessage);
        }

        public override void OnNewToken(string token)
        {
            base.OnNewToken(token);
            Preferences.Set("TokenFirebase", token);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除[Service][IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]代码编译正确

Eyn*_*ani 5

显然这是由于我对 Visual Studio 进行了更新,因为 android SDK 也更新了,解决方案是编辑[Services][Services(Exported = true)]android +31,留下这样的最终代码。

[Service(Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]

public class MyFirebaseMessagingService : FirebaseMessagingService
{
    readonly AndroidNotificationManager _androidNotification = new AndroidNotificationManager();
    public override void OnMessageReceived(RemoteMessage message)
    {
        var mensajeData = message.Data;

        string title= mensajeData["notiTitle"];
        string bodymessage= mensajeData["notiBody"];

        _androidNotification.CreateLocalNotification(title, bodymessage);
    }

    public override void OnNewToken(string token)
    {
        base.OnNewToken(token);
        Preferences.Set("TokenFirebase", token);
    }
}
Run Code Online (Sandbox Code Playgroud)

添加后,一切都正确编译

字体答案