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

Android 13已授予通知权限但通知仍不显示问题排查

问题描述

将原本在Android 7上正常运行的应用迁移至Android 13后,通知无法弹出。已按要求申请并成功获取POST_NOTIFICATIONS权限,但调用showNotification方法后通知仍不显示,无报错信息,该问题在模拟器和真机上均存在。相关源码片段如下:

public class MyActivity extends AppCompatActivity {

    private static final String CHANNEL_ID = "12345";
    public static final int NEWS_NOTIFICATION_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            ...
            // Prompt user to permit notifications
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
              if (!shouldShowRequestPermissionRationale(String.valueOf(PERMISSION_REQUEST_CODE))){
                getNotificationPermission();
              }
            }

        }

        ...

        public void getNotificationPermission(){
          try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.POST_NOTIFICATIONS},
                    PERMISSION_REQUEST_CODE);
            }
          } catch (Exception e) {
            Log.e(TAG, "Unable to request permission", e);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("Permission is granted in onRequestPermissionsResult()");
            } else {
                Log.d("Permission is not granted in onRequestPermissionsResult()");
            }
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    private void showNotification(String msg) {
        Intent resultIntent = new Intent(getApplicationContext(), MyActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent=null;
        resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_MUTABLE);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
            .setTicker("ticker")
            .setSmallIcon(R.drawable.app_icon90x90)
            .setContentTitle(msg)
            .setContentText("").setContentIntent(resultPendingIntent)
            .setAutoCancel(true)
            .build();
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NEWS_NOTIFICATION_ID, notification);
    }

    ...
}
排查方向及解决方法
  • 缺失通知渠道创建逻辑:Android 8.0(API 26)及以上版本要求所有通知必须归属到指定的通知渠道,未创建渠道的通知会被系统拦截。当前代码仅定义了CHANNEL_ID,但没有初始化渠道的步骤。
    新增渠道创建方法,并在onCreate或首次调用showNotification前执行:

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence channelName = "应用通知";
            String channelDesc = "接收应用推送通知";
            // 重要度设置为DEFAULT,确保通知能正常弹出
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
            channel.setDescription(channelDesc);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
    
  • 权限申请逻辑参数错误:shouldShowRequestPermissionRationale方法需要传入权限字符串(即Manifest.permission.POST_NOTIFICATIONS),而非请求码的字符串形式。当前代码的参数错误可能导致权限申请逻辑异常(即使你已成功授权,也建议修正以避免后续场景问题)。
    修正后的权限判断代码:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        if (!shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
            getNotificationPermission();
        }
    }
    
  • 通知内容为空:当前通知的setContentText("")设置了空文本,部分Android 13设备会过滤掉无实质内容的通知。建议设置非空的内容文本,例如:

    .setContentText("这是一条测试通知")
    
  • 通知图标问题:检查R.drawable.app_icon90x90是否符合Android通知图标的规范(要求透明背景、单色,避免复杂图案),不符合规范的图标可能导致通知无法正常显示。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:44:55