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

Unity Mobile Notification Plugin能否获取安卓其他应用的通知信息?

获取安卓其他应用通知的解决方案(Unity环境)

Unity官方的Mobile Notification Plugin仅支持监听本应用发送的通知,无法直接获取其他应用的通知。要实现跨应用通知监听,必须借助安卓原生的NotificationListenerService,以下是具体实现步骤:


1. 编写安卓原生监听服务类

创建一个继承自NotificationListenerService的Java类,用于捕获系统中所有应用的通知:

package com.yourcompany.yourapp;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.os.Bundle;

public class NotificationListener extends NotificationListenerService {
    public static NotificationReceivedCallback callback;

    public interface NotificationReceivedCallback {
        void onNotificationReceived(String packageName, String title, String text);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        // 过滤掉本应用自身的通知
        if (sbn.getPackageName().equals(getPackageName())) {
            return;
        }

        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString(android.app.Notification.EXTRA_TITLE);
        String text = extras.getString(android.app.Notification.EXTRA_TEXT);

        if (callback != null && title != null && text != null) {
            callback.onNotificationReceived(sbn.getPackageName(), title, text);
        }
    }
}

2. 配置安卓Manifest文件

在Unity项目的Assets/Plugins/Android/AndroidManifest.xml中添加权限和服务声明:

<!-- 添加必要权限 -->
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<!-- 注册通知监听服务 -->
<application>
    <service
        android:name=".NotificationListener"
        android:label="通知监听服务"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>
</application>

3. 编写Unity侧C#桥接代码

创建C#脚本,实现与安卓原生代码的交互,请求权限并接收通知回调:

using UnityEngine;
using TMPro;
using System;

public class NotificationListenerManager : MonoBehaviour
{
    public TMP_Text notificationDisplayText;
    private AndroidJavaObject nativeCallback;

    void Start()
    {
        // 初始化回调,接收原生层传来的通知数据
        nativeCallback = new AndroidJavaProxy("com.yourcompany.yourapp.NotificationListener$NotificationReceivedCallback",
            new Action<string, string, string>((packageName, title, text) =>
            {
                UpdateNotificationUI(packageName, title, text);
            }));

        // 将回调绑定到原生类
        AndroidJavaClass listenerClass = new AndroidJavaClass("com.yourcompany.yourapp.NotificationListener");
        listenerClass.SetStatic("callback", nativeCallback);

        // 检查并引导用户开启通知访问权限
        CheckNotificationPermission();
    }

    void CheckNotificationPermission()
    {
        AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaClass settingsClass = new AndroidJavaClass("android.provider.Settings");
        string packageName = currentActivity.Call<string>("getPackageName");
        string enabledListeners = settingsClass.CallStatic<string>("getString", currentActivity.Call<AndroidJavaObject>("getContentResolver"), "enabled_notification_listeners");

        if (!enabledListeners.Contains(packageName))
        {
            // 跳转到系统通知权限设置页面
            AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            currentActivity.Call("startActivity", intent);
        }
    }

    void UpdateNotificationUI(string packageName, string title, string text)
    {
        notificationDisplayText.text = $"收到其他应用通知:\n包名: {packageName}\n标题: {title}\n内容: {text}";
    }

    void OnDestroy()
    {
        // 清空静态回调,避免内存泄漏
        AndroidJavaClass listenerClass = new AndroidJavaClass("com.yourcompany.yourapp.NotificationListener");
        listenerClass.SetStatic("callback", null);
    }
}

关键注意事项

  • 权限要求:用户必须手动在系统设置中为应用开启「通知访问」权限,代码会自动引导用户进入设置页面
  • 版本兼容:NotificationListenerService仅支持安卓4.3(API 18)及以上版本,需确保项目目标SDK版本符合要求
  • 通知过滤:原生代码中已添加本应用通知过滤逻辑,避免重复监听自身发送的通知
  • 内存管理:在Unity脚本销毁时必须清空静态回调,防止内存泄漏

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 12:18:27