应用程序未构建 Android 12 (Firebase.Messaging)

Application does't buil whit Android 12 (Firebase.Messaging)

我有一个使用 Xamarin.FireBase.Messaging 的应用程序,它适用于所有 <12 android 版本。

当我尝试在 Android 12 中构建我的应用程序时出现此错误:

"*Severity Code Description Project File Line Suppression State Error ADB0010: Mono.AndroidTools.InstallFailedException: Unexpected install output: Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED: Failed parse during installPackageLI: /data/app/vmdl254956208.tmp/base.apk (at Binary XML file line #40): crc64bb777672d9471d38.CustomFirebaseMessagingService: Targeting S+ (version 31 and above) requires that an explicit value for android:exported be defined when intent filters are present] at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess(String output, String packageName) in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/Internal/AdbOutputParsing.cs:line 357 at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at AndroidDeviceExtensions.d__12.MoveNext() in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:line 206 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at AndroidDeviceExtensions.d__12.MoveNext() in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:line 223 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Xamarin.Android.Tasks.FastDeploy.d__105.MoveNext() in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/Tasks/FastDeploy.cs:line 339 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Xamarin.Android.Tasks.FastDeploy.d__105.MoveNext() in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/Tasks/FastDeploy.cs:line 356 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Xamarin.Android.Tasks.FastDeploy.d__100.MoveNext() in /Users/builder/azdo/_work/1/s/xamarin-android/external/monodroid/tools/msbuild/Tasks/FastDeploy.cs:line 213 0 *"

我认为我添加了所有属性“android:exported”。

这是我的清单:

manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.pushnotifications" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />


<application android:label="PushNotifications.Android" android:theme="@style/MainTheme">
    
    
    <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"/>

这是 CustomFirebaseMessagingService class:

[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class CustomFirebaseMessagingService : FirebaseMessagingService
{
    public readonly ILocalNotificationsService localNotificationsService;

    public CustomFirebaseMessagingService()
    {
        localNotificationsService = new LocalNotificationService();
    }

    public override void OnNewToken(string token)
    {
        base.OnNewToken(token);
        Log.Debug("FMC_SERVICE", token);
    }

    public override void OnMessageReceived(RemoteMessage message)
    {
        var notification = message.GetNotification();
        localNotificationsService.ShowNotification(notification.Title, notification.Body, message.Data);
    }

}

我能做什么?

我创建了一个简单的代码来测试你的代码。当我删除(Exported = false)时,我会遇到和你一样的错误。但如果我使用 [Service(Exported = false)],该项目运行良好。

所以应该不会出现这个问题。但是您可以尝试在 manifest.xml 中声明该服务。如:

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