How can NotificationListenerService used in MAUI?

Costas Athan 46 Reputation points
2024-07-06T13:25:53.9433333+00:00

I'm trying to listen to notifications of other apps and for that purpose I tried to utilize NotificationListenerService.

I have seen several examples from Xamarin, but nothing seems to work.

So how is it done in MAUI? How is the NotificationListenerService accessed?

Well, I had already declared the service in the Android manifest file.

<service android:name=".NotificationListener"
android:label="@string/NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
	<intent-filter>
		<action android:name="android.service.notification.NotificationListenerService" />
	</intent-filter>
</service>

Yet when I try to extend the class:

public class NLService : NotificationListenerService
{
    
}

I get the following error:

CS0246: The type or namespace name could not be found (are you missing a using directive or an assembly reference?)

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,623 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,546 Reputation points Microsoft Vendor
    2024-07-10T02:01:38.0933333+00:00

    Hello,

    I noticed that there is a duplicate thread on SO- How can NotificationListenerService used in MAUI? - Stack Overflow

    CS0246: The type or namespace name could not be found (are you missing a using directive or an assembly reference?

    Solution: adding conditional compilation

    #if ANDROID
    
    #endif
    

    See .NET MAUI invoking platform code - .NET MAUI | Microsoft Learn

    Best Regards,

    Wenyan Zhang


    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.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,016 Reputation points
    2024-07-06T19:52:29.6466667+00:00

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.