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

Android中onCreate()内Notification无法显示,求代码排查

Android通知不显示的问题排查与修复

以下是你代码中存在的几个关键问题,以及对应的修复方案:

1. Android 13+ 缺少通知权限

从Android 13(API 33)开始,发送通知必须申请POST_NOTIFICATIONS权限,否则系统会直接拦截通知。

修复步骤:

  • 在AndroidManifest.xml中添加权限声明:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  • 在代码中动态申请权限(在发送通知前执行):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        ActivityResultContracts.RequestPermission requestPermission = new ActivityResultContracts.RequestPermission();
        registerForActivityResult(requestPermission, isGranted -> {
            if (isGranted) {
                // 权限已授予,发送通知
                sendNotification();
            }
        }).launch(Manifest.permission.POST_NOTIFICATIONS);
        return;
    }
}

2. 未设置通知标题(setContentTitle)

虽然官方文档没有强制要求,但部分系统版本会因为缺少通知标题而不显示通知,或者显示异常。

3. 使用原生Notification.Builder导致兼容性问题

你分版本使用了原生的Notification.Builder,不如直接用NotificationCompat.Builder(你已经导入了该类),它会自动处理不同系统版本的适配,无需手动分版本逻辑。

4. 冗余的ChannelId设置(Android O+)

在Android O+的Notification.Builder构造方法中已经传入了ChannelId,再调用setChannelId(CHANNEL_ID)属于冗余操作,可以移除。


修改后的完整代码

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.activity.result.ActivityResultContracts;
import androidx.activity.result.RegisterForActivityResult;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.Manifest;

public class MainActivity extends AppCompatActivity {
    private static final String CHANNEL_ID = "all_notifications";
    private static final int NOTIFICATION_ID = 1245;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 检查并申请通知权限(Android 13+)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                ActivityResultContracts.RequestPermission requestPermission = new ActivityResultContracts.RequestPermission();
                registerForActivityResult(requestPermission, isGranted -> {
                    if (isGranted) {
                        sendNotification();
                    }
                }).launch(Manifest.permission.POST_NOTIFICATIONS);
                return;
            }
        }

        // 权限已存在或版本低于Android 13,直接发送通知
        sendNotification();
    }

    private void sendNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // 创建通知渠道(Android O+)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "通知渠道", NotificationManager.IMPORTANCE_HIGH);
            nm.createNotificationChannel(channel);
        }

        // 使用NotificationCompat.Builder构建通知,统一适配所有版本
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.small_icon)
                .setContentTitle("通知标题") // 添加标题
                .setContentText("New Message")
                .setSubText("subText")
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        nm.notify(NOTIFICATION_ID, builder.build());
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:02:44