Следуйте этому, удалите ненужный код из него. Это из активного проекта, может помочь вам, что не хватает.
fun createNormalNotification(notiId: Int, context: Context,
toClass: Class<out Activity>,
title: String, content: String,
smallIcon: Int) {
val intent = Intent(context, toClass)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(context, notiId
, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
creatingNotificationChannel(notificationManager, P.getChannelId(context), P
.getChannelName(context),
P.getChannelDescription(context))
val pattern = longArrayOf(500, 500, 500, 500, 500, 500, 500, 500, 500)
val mBuilder = NotificationCompat.Builder(context, P.getChannelId(context))
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setVibrate(pattern)
.setAutoCancel(true);
val notification = mBuilder.build()
notification.flags = Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT
notificationManager.notify(notiId, mBuilder.build());
}
@TargetApi(Build.VERSION_CODES.O)
private fun creatingNotificationChannel(notificationManager: NotificationManager,
channelId: String, channelName: String,
channelDescription: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val name: CharSequence = channelName;
val description = channelDescription
val channel = NotificationChannel(channelId, name, NotificationManager
.IMPORTANCE_DEFAULT);
channel.description = description;
// Register the channel with the system
notificationManager.createNotificationChannel(channel);
}
}