通知的基本用法 想要让手机弹出一条通知,只需要使用如下代码: val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notification = NotificationCompat.Builder(context, channelId) .setContentTitle("This is content title") .setContentText("This is content text") .setSmallIcon(R.drawable.small_icon) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon)) .build() manager.notify(1, notification)
想要指定通知的点击事件,只需要使用如下代码: val intent = Intent(this, NotificationActivity::class.java) val pi = PendingIntent.getActivity(this, 0, intent, 0) val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notification = NotificationCompat.Builder(context, channelId) .setContentTitle("This is content title") .setContentText("This is content text") .setSmallIcon(R.drawable.small_icon) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon)) .setContentIntent(pi) .build() manager.notify(1, notification)