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

Android通知应用无弹窗问题排查:我的代码哪里出错了?

问题排查与修复方案

以下是导致通知不显示的关键问题及对应修复方案:

1. 通知通道创建顺序错误

在 Android O(API 26)及以上版本中,必须先创建通知通道,再构建使用该通道的通知。你的代码先构建通知后创建通道,导致通知发送时通道尚未存在,系统直接忽略该通知。

2. CHANNEL_ID 包含多余空格

通道 ID 中的前导空格虽语法允许,但不符合标识符最佳实践,可能引发潜在匹配问题,建议使用无空格的规范 ID。

3. 缺少通知标题

Notification.Builder 未设置 setContentTitle(),部分 Android 版本会因缺少必要内容而不显示通知。

4. 未处理 Drawable 为空的情况

若 R.drawable.handbag 不存在或非 BitmapDrawable,强制类型转换会触发空指针异常,中断通知流程。


修复后的完整代码

package com.example.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private static final String CHANNEL_ID = "my_channel"; // 移除空格,规范ID
    private static final int NOTIFICATION_ID = 100;

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

        Bitmap largeIcon = null;
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.handbag, null);
        if (drawable instanceof BitmapDrawable) {
            largeIcon = ((BitmapDrawable) drawable).getBitmap();
        }

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            // 先创建通知通道
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "New channel", NotificationManager.IMPORTANCE_HIGH);
            nm.createNotificationChannel(channel);
            
            // 再构建通知
            notification = new Notification.Builder(this)
                    .setLargeIcon(largeIcon)
                    .setSmallIcon(R.drawable.handbag)
                    .setContentTitle("新通知") // 添加通知标题
                    .setContentText("new message")
                    .setSubText("new message for khan")
                    .setChannelId(CHANNEL_ID)
                    .build();
        } else {
            notification = new Notification.Builder(this)
                    .setLargeIcon(largeIcon)
                    .setSmallIcon(R.drawable.handbag)
                    .setContentTitle("新通知") // 添加通知标题
                    .setContentText("new message")
                    .setSubText("new message for khan")
                    .build();
        }

        // 发送通知
        nm.notify(NOTIFICATION_ID, notification);
    }
}

额外注意事项

  • 确保 R.drawable.handbag 资源存在于项目中,否则图标会失效。
  • 检查应用通知权限是否开启:在系统设置中找到你的应用,确认通知权限已启用。
  • Android 13(API 33)及以上版本需在 AndroidManifest.xml 中添加 <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />,并在运行时请求该权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 05:43:27