Home > Software design >  Error java.exe exited with code 1 Xamarin Firbase Messaging
Error java.exe exited with code 1 Xamarin Firbase Messaging

Time:06-13

I am using nuget package Xamarin.Firebase.Messaging and Xamarin.GooglePlayServices.Baseto receive push notifications in my app, previously it was working fine, but when I update visual studio 2022 to 17.2.3 it stopped working

I Tried all of these:

  • Update all nuget packages
  • delete obj bin folder from all shared projects
  • enable multidex
  • install and include

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

and nothing i did before has worked

my code to receive push notifications:

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);
        }
    }
}

if I remove [Service] or [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] the code compiles correctly

CodePudding user response:

Apparently it was due to the update I made of visual studio because the android SDK was also updated, the solution was to edit [Services] to [Services(Exported = true)] for android 31, leaving the final code like this.

[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);
    }
}

After adding that, everything compiled correctly

font answer

  • Related