I can't inherit from NotificationManager

Arzach 1 Reputation point
2021-03-15T19:53:54.66+00:00

I can't inherit from Android.App.NotificationManager:

 public class myNotificationManager : NotificationManager  
    {  
        protected myNotificationManager(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
        {  
      
        }  
    }  

I have no C# error, but when I build the project I get this error:

error: constructor NotificationManager in class NotificationManager cannot be applied to given types;  
public class myNotificationManager  
required: Context,Handler  
found: no arguments  
reason: actual and formal argument lists differ in length App2      
C:\Workspaces\Sandbox\App2\App2\obj\Debug\90\android\src\crc64bb1b55f618e11607\myNotificationManager.java  

I understand the message but according to the dotnet documentation: NotificationManager Constructor the type of the constructor's parameters is IntPtr and JniHandleOwnership not Context and Handler.

From the Android documentation I dont see any public constructor

Android target framework version: v9.0 (API 28)
Android SDK Plateform installed: 28,29,30

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-03-16T07:53:49.44+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    If we click into the source code of NotificationManager.cs in xamarin android ,we will find the function as follows:

    77979-image.png

    The type of class NotificationManager is static , so we can use NotificationManager in the following way:

            var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)  
                          {  
                              Description = description  
                          };  
             var notificationManager = (NotificationManager) GetSystemService(NotificationService);  
            notificationManager.CreateNotificationChannel(channel);  
    

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


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.