
本文详解在 android 13+ 设备上 notification 无法显示的根本原因及完整解决方案,涵盖通知渠道创建、运行时权限申请、兼容性处理与调试要点。
本文详解在 android 13+ 设备上 notification 无法显示的根本原因及完整解决方案,涵盖通知渠道创建、运行时权限申请、兼容性处理与调试要点。
在 Android 13(API level 33)及更高版本中,系统强制要求应用在首次显示通知前显式申请 POST_NOTIFICATIONS 运行时权限,否则调用 NotificationManager.notify() 将静默失败(不抛异常,但通知完全不出现)。这是您代码在 onCreate() 中无法触发通知的核心原因——即使通知渠道已正确创建、构建逻辑无误,缺少权限将直接导致通知被系统拦截。
✅ 正确实现步骤(含权限申请)
以下为适配 Android 13+ 的完整、可运行代码示例(已整合权限检查与请求逻辑):
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "all_notifications";
private static final int NOTIFICATION_ID = 1245;
private static final int PERMISSION_REQUEST_CODE = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Step 1: 检查并申请通知权限(仅 Android 13+ 需要)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
// 请求权限
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS},
PERMISSION_REQUEST_CODE);
return; // 等待权限回调后再发通知
}
}
// Step 2: 创建通知渠道(Android 8.0+ 必须)
createNotificationChannel();
// Step 3: 构建并发送通知
sendNotification();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "All Notifications";
String description = "Channel for general app notifications";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.enableLights(true);
channel.enableVibration(true);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private void sendNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.small_icon) // 确保该资源存在且为适应性图标(建议使用 Adaptive Icon)
.setContentTitle("New Message")
.setContentText("You have a new message")
.setSubText("subText")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (notificationManager.notify(NOTIFICATION_ID, builder.build())) {
Log.d("MainActivity", "Notification sent successfully");
} else {
Log.e("MainActivity", "Failed to send notification — check permissions and channel setup");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("MainActivity", "Notification permission granted");
sendNotification(); // 权限授予后立即发送通知
} else {
Toast.makeText(this, "Notification permission denied. Cannot show notifications.",
Toast.LENGTH_LONG).show();
Log.w("MainActivity", "Notification permission denied");
}
}
}
}
⚠️ 关键注意事项
-
权限声明:务必在 AndroidManifest.xml 中添加:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"></uses-permission>
(此权限为 normal 级别,无需在 manifest 中额外声明 android:maxSdkVersion)
-
图标资源:R.drawable.small_icon 必须是符合规范的 24×24 dp 透明背景 PNG(推荐使用 @mipmap/ic_notification 并通过 Image Asset Studio 生成自适应图标),否则在部分设备上可能显示为空白。
不要在 onCreate 中直接 notify():如上所示,必须先确保权限就绪再构建通知;若用户拒绝权限,应提供 UI 提示并引导至系统设置页(可使用 Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS))。
-
调试技巧:
- 使用 Logcat 过滤 NotificationManager 或 NotificationService 日志;
- 在 Android 13+ 设备上进入「设置 → 应用 → [您的App] → 通知」确认开关已开启;
- 避免在 onCreate() 中频繁调用 notify()(ID 冲突或系统限频可能导致丢弃)。
遵循以上方案,即可彻底解决 Android 13+ 下通知不显示的问题,并保持对旧版本系统的完全兼容。











