简单整理一下android通知,快速使用,不深入讲解。
需要完全掌握通知还请仔细学习官方文档。
参考官方文档:https://developer.android.com/training/notify-user/build-notification?hl=zh-cn
发一条简单通知:
记得开发送通知权限!!!
val CHANNEL_ID_1 = "10001"
private fun sendNotify() {
Log.d(TAG, "发送通知")
createNotificationChannel()
val intent = Intent(this, AlertActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val builder = NotificationCompat.Builder(this, CHANNEL_ID_1)
.setSmallIcon(R.mipmap.logo_splash)//设置通知小图标
.setContentTitle("我是通知标题")
.setContentText("我是通知内容")
.setContentIntent(pendingIntent)//设置通知跳转界面
.setPriority(NotificationCompat.PRIORITY_DEFAULT)//设置通知重要性
with(NotificationManagerCompat.from(this)) {
var notifyId=1//notifyId是标记通知的唯一id,不能重复 用于标记通知,移除指定通知等
notify(notifyId, builder.build())
}
}
//android 8以上需要创建通知频道才能发出通知
private 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 = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID_1, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
发一条自定义常驻通知:
记得开发送通知权限!!!
自定义通知就是可以让我们通知布局xml文件自定义通知的样式,监听通知中各个控件
private fun sendRemoteNotify() {
//还是要创建通知频道
createNotificationChannel()
val intent = Intent(this, AlertActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent1: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val pendingIntent2: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
//设置通知布局
val notificationLayout =
RemoteViews(packageName, R.layout.notify_my_first)
//设置布局中的控件1监听
notificationLayout.setOnClickPendingIntent(
R.id.img,
pendingIntent1
)
//设置布局中的控件2监听
notificationLayout.setOnClickPendingIntent(
R.id.tv,
pendingIntent2
)
//创建通知
val customNotifyBuilder = NotificationCompat.Builder(context, CHANNEL_ID_1)
.setSmallIcon(R.mipmap.logo_splash)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setOngoing(true)//设置为常驻通知
//发出通知
with(NotificationManagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(2, customNotifyBuilder.build())
}
}
通知布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/logo_splash" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="我是通知" />
</LinearLayout>