Android 14 之 常驻通知栏不再常驻

2,534 阅读2分钟

如果应用向用户显示不可关闭的前台通知,Android 14 修改部分场景下允许用户关闭此类通知。

这个就杀掉了一部分软件的流氓行为,不得不说,用户体验真的是非常重要。个人对于谷歌的此种行为还是特别认同的。

  • Android 14之前,通过Notification.Builder#setOngoing(true)或者NotificationCompat.Builder#setOngoing(true)设置电话,播放音乐等正在 执行的应用,为正在执行状态,这种Notification只能应用关闭,不能状态栏清除所有通知下被关闭。

  •   Android 14已经改变通知在设置FLAG_ONGOING_EVENT 使得用户可以关闭这一类通知。除了以下的情况:

    1. 当手机锁屏时
    2. 用户使用系统提供的Clear all按钮清除通知时
    3. 使用MediaStyle创建的通知。
    4. 安全和隐私相关政策限制的情况
    5. 设备策略控制器 (DPC) 和企业支持包
  • 以上几种都不会关闭设置常驻通知栏的通知。
//设置通知通道  
fun createNotificationChannel() {  
// Create the NotificationChannel, but only on API 26+ because  
// the NotificationChannel class is new and not in the support library  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
    val name = "troubleMaker"  
    val descriptionText = "Android 14 TEST"  
    val importance = NotificationManager.IMPORTANCE_DEFAULT  
    val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {  
        description = descriptionText  
    }  
// Register the channel with the system  
val notificationManager: NotificationManager =  
    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager  
    notificationManager.createNotificationChannel(channel)  
    }  
}  
  
fun showNotification(context: Context) {  
    intentMain = Intent()//这个intent会传给目标,可以使用getIntent来获取  
    intentMain.setClass(this, MainActivity2::class.java)  
    //用户点击该notification后才启动SecondActivity类  
    pendingIntent = PendingIntent.getActivity(this, 1, intentMain, PendingIntent.FLAG_IMMUTABLE);  
    // CHANNEL_ID:通道ID,可在类 MainActivity 外自定义  
    val builder = NotificationCompat.Builder(this, "CHANNEL_ID")  
        .setSmallIcon(R.mipmap.ic_launcher)  
        .setContentIntent(pendingIntent)  
        .setContentTitle("Android 14 测试")  
        .setContentText("进一步限制后台启动Activities")  
        .setOngoing(true)  
        .setCustomBigContentView(findViewById(R.layout.activity_main2))  
        // 通知优先级,可以设置为int型,范围-2至2  
        .setPriority(NotificationCompat.PRIORITY_MAX)  

    val build1 = builder.build()  
    with(NotificationManagerCompat.from(this)) {  
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS  ) != PackageManager.PERMISSION_GRANTED) {  
    Toast.makeText(applicationContext, "没有通知权限", Toast.LENGTH_SHORT).show()  
    ActivityCompat.requestPermissions(this@MainActivity3,arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1 )  
    // TODO: Consider calling  
    // ActivityCompat#requestPermissions  
    // here to request the missing permissions, and then overriding  
    // public void onRequestPermissionsResult(int requestCode, String[] permissions,  
    // int[] grantResults)  
    // to handle the case where the user grants the permission. See the documentation  
    // for ActivityCompat#requestPermissions for more details.  
    return  
    }  
        notify(2, build1)  
    }  
}

35c2a83c-a774-4eb5-bb95-dd68025a9fd1.gif