【问题标题】:.NET MAUI Firebase Messaging: Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when intent filter.NET MAUI Firebase 消息传递:针对 Android 12 及更高版本的应用程序需要在意图过滤器时为 `android:exported` 指定显式值
【发布时间】:2022-08-20 13:48:15
【问题描述】:

我正在尝试设置Xamarin.Firebase.Messaging,它说要指定android:exported,我尝试添加一些活动标签,我也发现了很多类似的问题,但在我的 .NET MAUI 项目中没有任何工作,或者我只是不知道如何请参阅MyFirebaseMessagingService.cs。 它引发了这个错误:

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  

当我从MyFirebaseMessagingService.cs 中删除[Service] 时,通知工作有效,但不调用方法OnMessageReceived

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

我的项目:

    标签: android firebase xamarin firebase-cloud-messaging .net-maui


    【解决方案1】:

    我遇到了这个问题,我将 Exported=false 添加到 Service 属性中,如下所示:

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

    生成的 AndroidManifest.xml 然后包含以下属性:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 2022-01-06
      • 2021-10-11
      • 2021-10-03
      • 2023-02-12
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多