通知相关的类及其获取和使用方式
1.NotificationManager
获取方式
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
2.Android 8.0及以后为了更好的管理通知泛滥的情况,NotificationChannel,创建方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("channelId","channelName",IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
}
3.Notification,创建方式如下
val intent = Intent(this,NotificationActivity::class.java)
val pi = PendingIntent.getActivity(this,0,intent,0)
val notification = NotificationCompat.Builder(this,"channelId")
.setContentText("notification content text")
.setContentTitle("notification title text")
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.ic_launcher_foreground))
.setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(
resources,R.drawable.ic_launcher_foreground)))
.setStyle(NotificationCompat.BigTextStyle().bigText("hahhahahhhhhhhahahhahhahahahahahhaaha" +
"hahhahahahahha"))
.setContentIntent(pi)
.setAutoCancel(true)
.build()
manager.notify(1,notification)