xamarin .NET MAUI Firebase消息传递:面向Android 12及更高版本的应用需要在intent filter时为'android:exported'指定显式值

dnph8jn4  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(206)

我试图设置Xamarin.Firebase.Messaging,它说要指定android:exported,我试图添加一些活动标记,我也发现了很多模拟问题,但在我的.NET MAUI项目中没有工作,或者我只是不知道如何引用MyFirebaseMessagingService.cs。它trowing此错误:

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

我的项目:

ykejflvf

ykejflvf1#

我遇到了这个问题,并将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>

希望这能有所帮助!

相关问题