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

Android应用通知被系统阻止无法启用,求技术解决方案

问题:Android应用通知被阻止无法开启

我正在开发一款Android应用,采用Java编写前端,Node.js作为后端,Postgres作为数据库。目前应用已实现认证功能,并通过相关API完成用户活动识别。但遇到的问题是,应用通知在设备设置中处于关闭且被阻止状态,我在不同模拟器及真机上尝试过,均无法开启被阻止的应用通知。

被阻止的通知

以下是我的Manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MTS"
        >
        <service
            android:name=".ARServices.ActivityRecognitionService"
            android:exported="false" />
        <service
            android:name=".ARServices.ActivityRecognitionBackgroundService"
            android:enabled="true"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.MTS">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".HomeActivity"
            android:exported="true"
            android:theme="@style/Theme.MTS">
        </activity>

        <service
            android:name=".ARServices.ActivityRecognitionService"
            android:exported="false" />
    </application>

</manifest>

请问该如何解决这个问题?有什么思路吗?

提前感谢!


解决方案思路

1. 补全Manifest中的通知权限声明

你的Manifest缺少Android 13+的通知权限,添加以下代码到<uses-permission>区域:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

低于Android 13的版本无需此权限,但仍需完成后续通知渠道的创建。

2. 强制创建通知渠道(Android 8.0+)

从Android 8.0(API 26)开始,所有通知必须归属一个通知渠道,否则系统会直接阻止应用通知功能。在应用启动时(比如MainActivity的onCreate)添加渠道创建代码:

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = getString(R.string.notification_channel_name);
        String channelDesc = getString(R.string.notification_channel_desc);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        
        NotificationChannel channel = new NotificationChannel("APP_MAIN_CHANNEL", channelName, importance);
        channel.setDescription(channelDesc);
        
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

记得在strings.xml中添加对应的渠道名称和描述字符串,后续发送通知时必须指定该渠道ID。

3. 动态请求通知权限(Android 13+)

Android 13及以上需要主动向用户请求通知权限,在合适的时机(比如应用首次启动、触发通知前)调用权限请求逻辑:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        ActivityResultLauncher<String> permissionLauncher = registerForActivityResult(
                new ActivityResultContracts.RequestPermission(),
                isGranted -> {
                    if (!isGranted) {
                        // 权限被拒绝,引导用户到设置页开启
                        Intent settingsIntent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                        settingsIntent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                        startActivity(settingsIntent);
                    }
                });
        permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
    }
}

4. 清理Manifest中的重复服务声明

你的Manifest里重复声明了ActivityRecognitionService,虽然不直接影响通知,但可能引发其他异常,建议删除其中一个重复的<service>节点。

5. 排查系统定制ROM限制

部分国产定制ROM(如小米、华为、OPPO)有额外的通知管控策略:

  • 检查应用是否被加入「省电模式」「后台冻结」列表,这类限制会导致通知被拦截
  • 手动进入应用信息页,确认「允许通知」开关是否被强制锁定,部分ROM需要手动解除限制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 13:36:00