Home > Blockchain >  .NET MAUI Firebase Messaging: Apps targeting Android 12 and higher are required to specify an explic
.NET MAUI Firebase Messaging: Apps targeting Android 12 and higher are required to specify an explic

Time:08-25

I am trying to setup Xamarin.Firebase.Messaging and it saying to specify android:exported, i tryed add some activity tag with that, also i found lot of simular problems but nothing work in my .NET MAUI project, or i just dont know how to refer to MyFirebaseMessagingService.cs. Its trowing this error:

Severity    Code    Description Project File    Line    Suppression State
Error   AMM0000 
    android:exported needs to be explicitly specified for element <service#crc64bdb245ab40403d31.MyFirebaseMessagingService>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
    FIrebasePushNotoficationsTest2  C:\Users\Lukas-PC\source\repos\FIrebasePushNotoficationsTest2\FIrebasePushNotoficationsTest2\obj\Debug\net6.0-android31.0\AndroidManifest.xml   28  

When i remove [Service] from MyFirebaseMessagingService.cs notofication works, but method OnMessageReceived is not called.

MyFirebaseMessagingService.cs:

using Android.App;
using Firebase.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FIrebasePushNotoficationsTest2.Platforms.Android
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.SendLog($"Msg:{message.GetNotification().Body}");
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="${applicationId}" />
      </intent-filter>
    </receiver>
  </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

MainActivity.cs:

using Android.App;
using Android.Content.PM;
using Android.OS;
using Firebase.Iid;
using Firebase.Messaging;

namespace FIrebasePushNotoficationsTest2;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        FirebaseMessaging.Instance.SubscribeToTopic("General");

        var t = FirebaseInstanceId.Instance.Token;
        Log.SendLog($"T:{t}");
    }
}

My project:

project

CodePudding user response:

I ran into this and I added Exported=false to the Service attribute like this:

[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MessagingService : FirebaseMessagingService
{
    public override void OnNewToken(string token)
    {
         base.OnNewToken(token);
    }
}

The generated AndroidManifest.xml then contains the attribute:

 <service android:exported="false" android:name="crc641fa5edea34be3b44.MessagingService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
 </service>

Hopefully this helps!

  • Related