How to add a locally created service in AndroidManifest.xml in .NET MAUI Android

Wei Wen 1,126 Reputation points
2022-11-08T21:15:21.247+00:00

I have Firebase set up on my .NET MAUI Android platform. I created a service class that extends FirebaseMessagingService. The reason for creating such a service is that I want to do my own processing in the override methods OnMessageReceived and OnNewToken. Now I need to add the service in AndroidManifest.xml file like this:

<service android:name="namespace.MyFirebaseMessagingService" android:exported="false">    
  <intent-filter>    
    <action android:name="com.google.firebase.MESSAGE_EVENT" />    
  </intent-filter>    
</service>    

"namespace" here is not the real text I entered, but it means the namespace that I used for this class. However, I got this error when the app looks for MyFirebaseMessagingService:

{Java.Lang.ClassNotFoundException: Didn't find class "namespace.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/~~ZXbo1KvMEFWsWjy6C_n6gA==/namespace-kgqC0jhhWsI6OJKLGVgeOg==/base.apk"],nativeLibraryDirectories…}  

I also tried this, and got the same error:
<service android:name=".MyFirebaseMessagingService" android:exported="false">

All the above methods to add a service is what is normally done in native Android. I can also do the same thing in Flutter and React Native. I wonder if it should be any different in .NET MAUI? BTW, My firebase is set up correctly. I can get firebase token in MainActivity.cs

Developer technologies .NET .NET MAUI
Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-11-09T01:37:58.517+00:00

    Hello,

    It is not recommended to modify AndroidManifest.xml both on MAUI and Xamarin.

    AndroidManifest.xml is a powerful file in the Android platform that allows you to describe the functionality and requirements of your application to Android. However, working with it is not easy. Xamarin.Android helps to minimize this difficulty by allowing you to add custom attributes to your classes, which will then be used to automatically generate the manifest for you. Our goal is that 99% of our users should never need to manually modify AndroidManifest.xml.

    You could refer to Working with the Android Manifest to get more details about how to to add custom attributes to your classes.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.