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

Android 13(API33)前台服务无通知显示问题排查求助

Android 13(Redmi Note 10 Pro)前台服务通知不显示的问题排查与修复

核心问题1:未动态申请POST_NOTIFICATIONS权限

Android 13(API 33)起,POST_NOTIFICATIONS属于危险权限,仅在Manifest中声明无法获得权限,必须主动向用户申请。默认状态下应用无此权限,会导致所有通知(包括前台服务通知)无法显示。

修复代码:

在启动服务前先检查并请求权限:

private static final int REQUEST_POST_NOTIFICATIONS = 1001;

private void runService() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                != PackageManager.PERMISSION_GRANTED) {
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                if (isGranted) {
                    startServiceInternal();
                } else {
                    Toast.makeText(this, "需开启通知权限才能显示服务通知", Toast.LENGTH_SHORT).show();
                }
            }).launch(Manifest.permission.POST_NOTIFICATIONS);
            return;
        }
    }
    startServiceInternal();
}

private void startServiceInternal() {
    Log.d(TAG, "Starting service");
    Intent serviceIntent = new Intent(this, MyScannerService.class);
    serviceIntent.setAction(MyScannerService.ACTION_START_SERVICE);
    startForegroundService(serviceIntent);
    Log.d(TAG, "Started");
}

核心问题2:MIUI系统的特殊管控

Redmi设备的MIUI系统对后台应用和通知有额外限制,即使权限申请通过,也可能默认关闭应用的通知展示或前台服务权限。

手动检查与设置:

  1. 进入手机「设置」→「通知与控制中心」→「通知管理」,找到你的应用,确保允许通知、「悬浮通知」选项处于开启状态。
  2. 进入应用「应用信息」→「权限管理」,确认「前台服务」权限已开启,若有「后台弹出界面」权限也需允许。

次要问题:代码冗余与潜在风险

1. 重复创建通知渠道

每次调用createNotification()都会重复创建通知渠道,虽然系统会忽略重复操作,但建议仅在服务启动时创建一次:

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate(): The service has been created");
    _waiter = new Waiter();
    
    // 仅创建一次通知渠道
    createNotificationChannel();
    
    Notification notification = createNotification();
    startForeground(MY_SERVICE_ID, notification);
}

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(MY_CHANNEL_ID, MY_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("My test channel");
        channel.enableLights(true);
        channel.setSound(null, null);
        channel.setShowBadge(true);
        channel.setLightColor(Color.BLUE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }
}

private Notification createNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
    return new NotificationCompat.Builder(this, MY_CHANNEL_ID)
            .setContentTitle("My Service scream")
            .setContentText("Some important text...")
            .setTicker("My ticker")
            .setSmallIcon(R.drawable.ic_clock)
            .setContentIntent(pendingIntent)
            .setOngoing(true) // 前台服务通知建议设为持续显示,避免被自动清除
            .build();
}

2. 停止服务的Intent调用错误

停止服务无需使用startForegroundService(),直接调用startService()即可:

private void stopService() {
    Log.d(TAG, "Stopping service");
    Intent serviceIntent = new Intent(this, MyScannerService.class);
    serviceIntent.setAction(MyScannerService.ACTION_STOP_SERVICE);
    startService(serviceIntent); // 替换startForegroundService()为startService()
    Log.d(TAG, "Stopped");
}

3. 小图标兼容性问题

MIUI对通知小图标要求严格,VectorDrawable可能无法正常显示,建议替换为白色轮廓、透明背景的png格式图标(符合Android图标尺寸规范)。

验证步骤

  1. 重新运行应用,确保通知权限申请通过。
  2. 检查MIUI的应用权限和通知设置是否正确。
  3. 启动服务后查看通知栏是否显示前台服务通知。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 19:23:08