0

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

Eyner Mamani
  • 183
  • 1
  • 14
  • Code 1 can happen for various reasons, try to set the build output verbosity to "Diagnostic" and check the build output again, the error should be more detailed. This setting is located at: Tools → Options → Projects and Solutions → Build and Run → MSBuild project build output verbosity – Luccas Clezar Jun 10 '22 at 18:18

2 Answers2

5

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

Eyner Mamani
  • 183
  • 1
  • 14
1

In addition to the answers here, I also had to export the broadcast reviver

[BroadcastReceiver(Enabled = true, Exported = true)]
Legit007
  • 181
  • 1
  • 8