Android 14(API34)如何让通知超时覆盖系统默认设置?
Android 14 通知超时覆盖问题
我在运行Android 14(API34)的手机上测试一款通知应用,遇到的问题是通知仅显示几秒就消失(只留下通知点),我需要让它在屏幕上停留约2分钟。
手机的“设置-无障碍-计时控制-操作响应时间”可以修改全局默认超时,但不想让用户手动操作,希望通过代码覆盖该设置延长通知显示时长。
我已经在Java代码中添加了setOngoing(true)和setTimeoutAfter(120000),代码如下:
void createPopUpNotification(int nfNo){ if (SDK_INT >= Build.VERSION_CODES.O) { String NOTIFICATION_CHANNEL_ID; NOTIFICATION_CHANNEL_ID = "AMJnotifychannel"; Intent goShow = new Intent(this, DisplayActivity.class); goShow.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, goShow, FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT ); RemoteViews expandedView=new RemoteViews(getApplicationContext().getPackageName(),R.layout.expandednotify); expandedView.setTextViewText(R.id.placename,placeName); NotificationManager notificationManager = (NotificationManager) this.getSystemService( NOTIFICATION_SERVICE ) ; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); if (SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "amjchannel", NotificationManager.IMPORTANCE_HIGH); channel.shouldVibrate(); channel.enableLights(true); channel.enableVibration(true); channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC); notificationManager.createNotificationChannel(channel); } builder.setSmallIcon(R.mipmap.amjnotify); builder.setContentIntent(pendingIntent); builder.setCustomContentView(expandedView); builder.setCustomBigContentView(expandedView); builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setOngoing(true); builder.setAutoCancel(true); builder.setTimeoutAfter(120000); builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ; notificationManager.notify(nfNo, builder.build()); } }
但这段代码无法覆盖系统标准超时,当手机“操作响应时间”设为默认时,通知还是几秒后消失。请问有什么方法能让应用覆盖手机的通知默认超时设置?
解决方案
1. 移除冲突的属性配置
你的代码中同时设置了setOngoing(true)和setAutoCancel(true),这两个属性存在冲突:
setOngoing(true)标记通知为持续型,这类通知默认不会自动消失,且用户无法手动清除(需应用主动取消)setAutoCancel(true)会让通知在用户点击后自动取消,同时会干扰持续型通知的停留逻辑
保留setOngoing(true)并移除setAutoCancel(true),这是让通知稳定停留的基础。
2. 修正通知渠道的核心配置
Android 12及以上版本,通知的行为优先级由通知渠道决定,而非Builder的单独设置:
- 确保渠道设置为
IMPORTANCE_HIGH,并补充强提示属性(振动、灯光、铃声),让系统判定为高优先级通知,避免被快速收起 - Android 14(API34)新增了渠道级的超时配置
setNotificationTimeout,需针对性适配
修改渠道创建代码:
if (SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "amjchannel", NotificationManager.IMPORTANCE_HIGH); // 明确配置振动模式 channel.enableVibration(true); channel.setVibrationPattern(new long[]{100, 200, 300}); // 配置呼吸灯 channel.enableLights(true); channel.setLightColor(Color.RED); channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC); // Android 14+ 渠道级超时设置 if (SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { channel.setNotificationTimeout(120000); } notificationManager.createNotificationChannel(channel); }
3. 避免重复创建渠道导致配置失效
通知渠道一旦创建,后续修改配置不会自动生效(除非用户清除应用数据),需先检查渠道是否存在再做处理:
if (SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel existingChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID); if (existingChannel == null) { // 创建新渠道并配置属性 NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "amjchannel", NotificationManager.IMPORTANCE_HIGH); // 配置振动、灯光等属性... if (SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { channel.setNotificationTimeout(120000); } notificationManager.createNotificationChannel(channel); } else { // 更新现有渠道的可修改属性 existingChannel.setImportance(NotificationManager.IMPORTANCE_HIGH); existingChannel.setVibrationPattern(new long[]{100, 200, 300}); if (SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { existingChannel.setNotificationTimeout(120000); } notificationManager.createNotificationChannel(existingChannel); } }
4. 最终Builder代码调整
移除冲突的setAutoCancel(true),确保配置统一:
builder.setSmallIcon(R.mipmap.amjnotify); builder.setContentIntent(pendingIntent); builder.setCustomContentView(expandedView); builder.setCustomBigContentView(expandedView); builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setOngoing(true); builder.setTimeoutAfter(120000); builder.setChannelId(NOTIFICATION_CHANNEL_ID); notificationManager.notify(nfNo, builder.build());
内容的提问来源于stack exchange,提问作者Chris S
相关产品推荐
相关产品推荐

