You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MAUI中使用NotificationListenerService监听应用通知?

在MAUI中实现NotificationListenerService监听通知

一、修正服务类定义

你当前的服务类注册属性存在错误,无需手动指定Register属性,MAUI会自动处理Android可调用包装器(ACW)。正确的服务类写法如下:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Service.Notification;
using Android.Util;

namespace YourMauiAppName.Platforms.Android; // 替换为你MAUI项目的Android平台命名空间

[Service(Label = "通知监听服务", Permission = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE")]
[IntentFilter(new[] { "android.service.notification.NotificationListenerService" })]
public class NLService : NotificationListenerService
{
    public override void OnCreate()
    {
        base.OnCreate();
        Log.Info("NotificationListener", "服务已创建");
    }

    public override void OnNotificationPosted(StatusBarNotification sbn)
    {
        base.OnNotificationPosted(sbn);
        // 处理收到的通知
        var packageName = sbn.PackageName;
        var notificationTitle = sbn.Notification.Extras.GetString(Android.App.Notification.ExtraTitle);
        var notificationContent = sbn.Notification.Extras.GetString(Android.App.Notification.ExtraText);
        
        Log.Info("NotificationReceived", $"应用:{packageName},标题:{notificationTitle},内容:{notificationContent}");
    }

    public override void OnNotificationRemoved(StatusBarNotification sbn)
    {
        base.OnNotificationRemoved(sbn);
        // 处理通知被移除的逻辑
    }
}

二、修正AndroidManifest.xml配置

你当前的AndroidManifest.xml中服务名称不正确,MAUI中Android平台类会编译到特定命名空间下,不能直接使用.NotificationListener。正确做法是无需手动在Manifest中注册服务,因为上面的[Service]和[IntentFilter]属性会自动生成对应的Manifest配置。

若你手动维护AndroidManifest.xml,需删除原有<service>节点避免冲突,最终保留权限声明即可:

<?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:supportsRtl="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
</manifest>

三、引导开启通知监听权限

NotificationListenerService需要用户手动在系统设置中开启权限,可在MAUI共享项目中添加跳转逻辑:

using Microsoft.Maui.ApplicationModel;

public static void OpenNotificationListenerSettings()
{
    if (DeviceInfo.Platform == DevicePlatform.Android)
    {
        var intent = new Android.Content.Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        intent.AddFlags(Android.Content.ActivityFlags.NewTask);
        Android.App.Application.Context.StartActivity(intent);
    }
}

在应用启动或按钮点击等时机调用该方法,提示用户开启权限。

四、验证服务运行

  1. 安装应用后,调用上述方法跳转到设置页面,找到你的应用并开启通知监听权限。
  2. 发送测试通知(如其他应用消息),查看Logcat日志,确认OnNotificationPosted是否被触发。

常见问题排查

  • 服务未启动:检查权限是否开启、服务类命名空间是否正确、[Service]属性配置是否无误。
  • OnNotificationPosted未触发:部分系统或应用会限制通知监听,确保测试应用的通知未开启静默模式。

内容的提问来源于stack exchange,提问作者Costas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:11:19