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

Android Studio中应用通知栏不显示问题解决求助

Android通知栏不显示通知问题排查与解决

问题描述

我在Android Studio中编写代码,为应用创建Notification Channel实现通知功能,但点击触发按钮后,通知栏并未显示通知。点击按钮后通知栏无任何提示:

通知栏无提示截图

原代码

package com.example.notification_example;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {


NotificationManager notification;
Button btn;
String ChannelID = "ChannelID";
String ChannelName = "ChannelName";

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

    btn = findViewById(R.id.button);
    notification = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(ChannelID,
                        ChannelName,
                        NotificationManager.IMPORTANCE_DEFAULT);
                notification.createNotificationChannel(notificationChannel);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, ChannelID);

                builder.setSmallIcon(R.drawable.hand_notification)
                        .setContentTitle("Updates").setContentText("Updates Are ready to install");
                notification.notify(1, builder.build());
            }
        }
    });
}
}

问题排查与解决步骤

1. 修复版本判断逻辑

原代码中Build.VERSION.SDK_INT > Build.VERSION_CODES.O的判断错误,Android O(API 26)及以上版本就需要创建通知渠道,应改为>=,否则API 26的设备不会执行渠道创建逻辑,导致通知无法显示。

2. 调整通知渠道创建时机

不要在按钮点击事件中重复创建渠道,将渠道创建逻辑移到onCreate方法中,系统会自动忽略重复创建的渠道,避免不必要的操作。

3. 添加Android 13+通知权限申请

Android 13(API 33)及以上版本需要动态申请POST_NOTIFICATIONS权限,否则应用无法发送通知,需在Manifest中添加权限并实现动态申请逻辑。

4. 检查通知小图标有效性

确保R.drawable.hand_notification是符合Android规范的通知小图标:要求为白色透明背景的矢量图,否则可能因图标问题导致通知不显示。

修改后的完整代码

Java代码

package com.example.notification_example;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    NotificationManager notificationManager;
    Button btn;
    String ChannelID = "ChannelID";
    String ChannelName = "ChannelName";
    private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 100;

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

        btn = findViewById(R.id.button);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // 提前创建通知渠道
        createNotificationChannel();

        // Android 13+ 检查通知权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATION_PERMISSION_REQUEST_CODE);
            }
        }

        btn.setOnClickListener(view -> sendNotification());
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(ChannelID,
                    ChannelName,
                    NotificationManager.IMPORTANCE_DEFAULT);
            // 添加渠道描述,帮助用户识别渠道用途
            notificationChannel.setDescription("应用更新通知专用渠道");
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    private void sendNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, ChannelID);

        builder.setSmallIcon(R.drawable.hand_notification)
                .setContentTitle("更新提示")
                .setContentText("有更新已准备好安装")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        notificationManager.notify(1, builder.build());
    }

    // 处理权限申请结果
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                sendNotification();
            } else {
                Toast.makeText(this, "需要开启通知权限才能显示通知", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

清单文件(AndroidManifest.xml)添加权限

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

内容的提问来源于stack exchange,提问作者MMd.NrC

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:30:02