Notification各个版本知识点记录
可参考以下两个链接:
https://juejin.cn/post/7113509911887085581#heading-0
https://github.com/yechaoa/MaterialDesign/blob/master/app/src/main/java/com/yechaoa/materialdesign/activity/NotificationActivity.kt
关键类:
NotificationManager
通知管理器,用来发起、更新、删除通知
NotificationChannel
通知渠道,8.0及以上配置渠道以及优先级
NotificationCompat.Builder
通知构造器,用来配置通知的布局显示以及操作相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Intent intent = new Intent(this, TestNotifyActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME , NotificationManager.IMPORTANCE_HIGH); channel.setShowBadge(true); manager.createNotificationChannel(channel); } notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("stew~~") .setContentText("1234567890-=") .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pi) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .build(); manager.notify(101, notification);
|
- android8 需要添加通道NotificationChannel
- android10 可以添加操作action(addAction)
- android12.0及以上,可以设置需要解锁设备才能操作:setAuthenticationRequired
- android12.0开始,将不支持完全自定义的通知,会提供 Notification.DecoratedCustomViewStyle替代…
- android12.0 需要明确设置flag,否则会有报错:
1 2 3 4 5
| mFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { PendingIntent.FLAG_IMMUTABLE } else { PendingIntent.FLAG_UPDATE_CURRENT }
|