Android 如何在安卓系统中发出不会自行崩溃的通知?
通知现在在2秒后显示并隐藏在状态栏中。如何使通知不进入状态栏Android 如何在安卓系统中发出不会自行崩溃的通知?,android,Android,通知现在在2秒后显示并隐藏在状态栏中。如何使通知不进入状态栏 val builder = NotificationCompat.Builder(context, "Over limit channel") .setContentTitle("") .setContentText("") .setGroupSummary(false) .setSmallIcon(R
val builder = NotificationCompat.Builder(context, "Over limit channel")
.setContentTitle("")
.setContentText("")
.setGroupSummary(false)
.setSmallIcon(R.mipmap.launcher_icon)
.setContent(remoteViews)
.setAutoCancel(false)
.setGroup("false")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(800, builder.build())
您可以使用所需的通知启动前台服务,也可以将通知设置为正在进行的事件以使其不可拒绝我发现的诀窍是使用方法
setFullScreenIntent(android.app.PendingIntent,boolean highPriority)
使用此方法,通知将永远不会从状态栏中删除,除非您自己这样做
以下是一个例子:
val intent = Intent()
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val builder = NotificationCompat.Builder(context, "Over limit channel")
.setContentTitle("")
.setContentText("")
.setGroupSummary(false)
.setSmallIcon(R.mipmap.launcher_icon)
.setContent(remoteViews)
.setAutoCancel(false)
.setGroup("false")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(pendingIntent,true)
来自android Studio文档:
启动而不是将通知发布到状态的意图
酒吧。仅适用于要求极高优先级的通知
用户的即时关注,如来电或
用户已明确设置为特定时间的闹钟。如果
此设施用于其他用途,请向用户提供
选项将其关闭并使用正常通知,因为这可能是
极具破坏性。在某些平台上,系统UI可能会选择
显示提示通知,而不是启动此意图,
当用户正在使用设备时
Params:intent–挂起的启动意图。高优先级-通过
true将导致发送此通知,即使其他
通知被抑制
更新
要使某些API版本起作用,您必须在清单上添加此权限
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"></uses-permission>
感谢您的帮助,但我不需要永久性通知,我需要一个不会自行崩溃的常规通知。如果使用“正在进行”功能,用户将无法从操作中心滑动应用程序。感谢您的帮助,但它没有帮助,通知会崩溃到状态栏(我已经在android api 28上测试了这段代码,但没有在api上测试30@YuriPetrov您测试了这段代码的哪个版本?@YuriPetrov请检查我更新的答案,现在它在api 29和30上工作了。非常感谢,一切顺利,您帮了我很多忙!!!